def keyPressEvent(self, event: QKeyEvent): clipboard = QApplication.clipboard() if event.matches(QKeySequence.Copy): print("Ctrl + C") clipboard.setText("some text") if event.matches(QKeySequence.Paste): print("Clip Event : ", end="") mime = clipboard.mimeData() if mime.hasImage(): print("Image") self.currentImage = imgFormat( QImageTocvmat(clipboard.image()), self.defaultLevel, None, (self.width, self.height), ) self.requestImage(self.currentImage) elif mime.hasUrls(): print("URL") elif mime.hasText(): print("Text") self.currentImage = imgFormat( self.fetchImage(clipboard.text()), self.defaultLevel, None, (self.width, self.height), ) self.requestImage(self.currentImage) else: print("Unknown type")
def eventFilter(self, Object, event): if Object == self.passEdit: if event.type() == QEvent.MouseMove or event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches(QKeySequence.Copy): return True return super().eventFilter(self, Object, QEvent)
def eventFilter(self, object, event): if object == self.edit:#这里是对事件的判断。其中QKeyEvent类描述了一个关键事件。当按下或释放按键时,主要事件将发送到具有键盘输入焦点的小部件。然后运用matches方法匹配具体的按键。 if event.type() == QEvent.MouseMove or event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches(QKeySequence.Copy) or key.matches(QKeySequence.Paste):#然后进行事件判断与转换:鼠标移动对应的事件类型为QEvent.MouseMove,鼠标双击对应的事件类型为QEvent.MouseButtonDblClick,全选、复制、粘贴对应的事件类型为 QEvent.KeyPress,当接收到这些事件时,需要被过滤掉,所以返回true。 return True return QDialog.eventFilter(self, object, event)#继续传递该事件到被观察者,由其本身调用相应的事件
def eventFilter(self, object, event): if object == self.edit: if event.type() == QEvent.MouseMove or event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches(QKeySequence.Copy) or key.matches(QKeySequence.Paste): return True return QDialog.eventFilter(self, object, event) #继续传递该事件到被观察者,由其本身调用相应的事件
def keyPressEvent(self, event: QKeyEvent): if self.window().is_dirty() \ and (event.matches(QKeySequence.MoveToNextLine) or event.matches(QKeySequence.MoveToPreviousLine)): veto = self.window().central_widget.promptToSave() if not veto: QTreeWidget.keyPressEvent(self, event) else: event.ignore() else: QTreeWidget.keyPressEvent(self, event)
def eventFilter(self, object, event): if object == self.edit: # 如果接受事件的对象是QLineEdit生成的对象 if event.type() == QEvent.MouseMove or event.type( ) == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches(QKeySequence.Copy) or \ key.matches(QKeySequence.Paste): return True return QDialog.eventFilter(self, object, event)
def keyPressEvent(self, event: QKeyEvent): char = event.text() if (char and ord(char) in range(ord(' '), ord('~') + 1) or char.isalpha()) \ and (event.modifiers() in (Qt.NoModifier, Qt.ShiftModifier) or event.modifiers() & Qt.AltModifier): if event.modifiers() & Qt.AltModifier: return if not (set(char) - (VALID_CHARS | set('vV'))): self.insert(char) else: if event.matches(QKeySequence.Cancel): self.clear() elif event.matches(QKeySequence.Copy): if not self.text(): return if self.selectedText(): text_to_copy = self.selectedText() else: text_to_copy = self.text() text_to_copy = toEditableExpr(text_to_copy) clipboard = QApplication.clipboard() clipboard.setText(text_to_copy) elif event.matches(QKeySequence.Paste): clipboard = QApplication.clipboard() text = clipboard.text() if not text: return self.insert(text.strip()) elif event.matches(QKeySequence.Cut): if not self.text(): return if self.selectedText(): text_to_copy = self.selectedText() super().keyPressEvent(event) else: text_to_copy = self.text() self.clear() text_to_copy = toEditableExpr(text_to_copy) clipboard = QApplication.clipboard() clipboard.setText(text_to_copy) else: super().keyPressEvent(event)
def keyPressEvent(self, event: QKeyEvent): if event.matches(QKeySequence.Copy): selected_indexes = [ (x.row(), x.column()) for x in self.selectionModel().selectedIndexes() ] _logger.info('Copying the following items to the clipboard: %r', selected_indexes) # Dicts are ordered now. Yay! by_row = {} for row, column in sorted(selected_indexes): by_row.setdefault(row, []).append(column) out_strings = [] for row, column_list in by_row.items(): out_strings.append('\t'.join([ self._model.render_item_for_clipboard( self._model.index(row, col)) for col in column_list ])) if out_strings: QApplication.clipboard().setText(os.linesep.join(out_strings)) else: super(_TableView, self).keyPressEvent(event)
def keyPressEvent(self, e: QtGui.QKeyEvent) -> None: if e.matches(QtGui.QKeySequence.Delete): rows: List[int] = list() for index in self.selectedIndexes(): row = index.row() if row not in rows: rows.append(row) idList = [] for row in rows: data = self.model().getData()[row] idList.append(data["key"]) self.requestDelete(idList) #elif e.key() == QtCore.Qt.Key_Return and e.modifiers() == QtCore.Qt.AltModifier: # print("ALT RETURN!!") # for index in self.selectedIndexes(): # print(index) # print(index.row()) # print(index.column()) # self.openPersistentEditor(index) # break elif e.key() == QtCore.Qt.Key_Return: for index in self.selectedIndexes(): self.itemActivated.emit(self.model().getData()[index.row()]) else: super(ListTableView, self).keyPressEvent(e)
def eventFilter(self, object, event): ''' 鼠标移动对应的事件类型为QEvent.MouseMove, 鼠标双击对应的事件类型为QEvent.MouseButtonDblClick, 全选、复制、粘贴对应的事件类型为 QEvent.KeyPress,当接收到这些事件时,需要被过滤掉,返回true。 ''' if object == self.edit: if event.type() == QEvent.MouseMove or event.type( ) == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches( QKeySequence.Copy) or key.matches(QKeySequence.Paste): return True return QDialog.eventFilter(self, object, event) #继续传递该事件到被观察者,由其本身调用相应的事件
def keyPressEvent(self, e: QtGui.QKeyEvent) -> None: """ Catch and handle QKeySequence.Delete. """ if e.matches(QtGui.QKeySequence.Delete): for index in self.selectionModel().selection().indexes(): self.model().removeItemAtIndex(index) else: super(SelectedEntitiesWidget, self).keyPressEvent(e)
def eventFilter(self, object, event): # eventFilter must put in the same thread with bond object # if not in the same thread, nothing will be done if object == self.edit: if event.type() == QEvent.MouseMove or event.type( ) == QEvent.MouseButtonDblClick: # return True for stopping next operation # otherwise return False return True # if delete received object in eventFilter(), must return True # if return False, Qt will sender event to deleted object, which will lead to a crash elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches(QKeySequence.Copy) or \ key.matches(QKeySequence.Paste): return True # it is important to add this return, and it will return this event to QLineEdit object # keep sending this event to bond object return QDialog.eventFilter(self, object, event)
def keyPressEvent(self, event: QKeyEvent): if event.matches(QKeySequence.Paste): text = QApplication.clipboard().text() # print(text) list = text.splitlines() for i, r in enumerate(list): print(i, r) item = QListWidgetItem(r) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.EditRole | Qt.PlainText | Qt.TextEditorInteraction | Qt.TextEditable | Qt.TextSelectableByKeyboard) self.theList.addItem(item)
def keyPressEvent(self, e: QKeyEvent): if e.matches(QKeySequence.MoveToPreviousLine): pass elif e.matches(QKeySequence.MoveToNextLine): pass elif e.matches(QKeySequence.Paste): pass elif e.matches(QKeySequence.InsertParagraphSeparator): if not self.multi_line_mode: cmd = self.getCommand() self.push_command.emit(cmd) self.prompted = False self.checkEditable() else: super().keyPressEvent(e) elif e.matches(self.multi_line_return): if self.multi_line_mode: cmd = self.getCommand() cmd.replace('\u2029', '\n') self.push_command.emit(cmd) self.prompted = False self.checkEditable() else: super().keyPressEvent(e)
def keyPressEvent(self, event: QKeyEvent): if event.key() in [Qt.Key_Return, Qt.Key_Enter]: # Add the input data into the List Widget theText = self._serialNumItem.text() item = QListWidgetItem(theText) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.PlainText) self._serialNumList.addItem(item) self._serialNumItem.clear() if event.key() in [Qt.Key_Delete]: # Remove the selected data from the List Widget row = self._serialNumList.currentRow() self._serialNumList.takeItem(row) if event.matches(QKeySequence.Paste): # Add the clipboard data into the List Widget theText = QApplication.clipboard().text() # print(theText) theList = theText.splitlines() for i, r in enumerate(theList): print(i, r) item = QListWidgetItem(r) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable | Qt.PlainText) self._serialNumList.addItem(item)
def keyPressEvent(self, event: QKeyEvent): if event.matches(QKeySequence.Copy): selected_rows = [ x.row() for x in self._table.selectionModel().selectedIndexes() ] _logger.info('Copying the following rows to the clipboard: %r', selected_rows) if len(selected_rows) == 1: out_strings = [self._table.item(selected_rows[0], 0).text()] else: out_strings = [] for row in selected_rows: header = self._table.verticalHeaderItem(row).text() data = self._table.item(row, 0).text() out_strings.append(f'{header}\t{data}') if out_strings: QApplication.clipboard().setText(os.linesep.join(out_strings)) else: super(LittleBobbyTablesWidget, self).keyPressEvent(event)
def eventFilter(self, object, event): ''' 事件过滤器,主要是对密码输入框进行操作的。 ''' if object == self.lineEdit_2: if event.type() == QEvent.MouseMove or event.type( ) == QEvent.MouseButtonDblClick: return True # 鼠标移动、鼠标双击过滤掉 elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches( QKeySequence.Copy) or key.matches(QKeySequence.Paste): return True # 键盘全选、复制、粘贴快捷键过滤掉 elif object == self.lineEdit_3: # 同上 if event.type() == QEvent.MouseMove or event.type( ) == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: key = QKeyEvent(event) if key.matches(QKeySequence.SelectAll) or key.matches( QKeySequence.Copy) or key.matches(QKeySequence.Paste): return True return QDialog.eventFilter(self, object, event)
def keyPressEvent(self, event: QKeyEvent): if event.key() == Qt.Key_Delete: min_row, max_row, start, end = self.selection_range() if min_row == max_row == start == end == -1: return self.setEnabled(False) self.setCursor(Qt.WaitCursor) self.model().delete_range(min_row, max_row, start, end - 1) self.unsetCursor() self.setEnabled(True) self.setFocus() if event.matches(QKeySequence.Copy): self.on_copy_action_triggered() return if event.key() == Qt.Key_Space: min_row, max_row, start, _ = self.selection_range() if start == -1: return self.model().insert_column(start, list(range(min_row, max_row + 1))) if event.key() not in (Qt.Key_Right, Qt.Key_Left, Qt.Key_Up, Qt.Key_Down) \ or event.modifiers() == Qt.ShiftModifier: super().keyPressEvent(event) return min_row, max_row, min_col, max_col = self.selection_range() if min_row == max_row == min_col == max_col == -1: super().keyPressEvent(event) return max_col -= 1 scroll_to_start = True if event.key( ) == Qt.Key_Right and max_col < self.model().col_count - 1: max_col += 1 min_col += 1 scroll_to_start = False elif event.key() == Qt.Key_Left and min_col > 0: min_col -= 1 max_col -= 1 elif event.key( ) == Qt.Key_Down and max_row < self.model().row_count - 1: first_unhidden = -1 for row in range(max_row + 1, self.model().row_count): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = max_row - min_row max_row = first_unhidden min_row = max_row - sel_len scroll_to_start = False elif event.key() == Qt.Key_Up and min_row > 0: first_unhidden = -1 for row in range(min_row - 1, -1, -1): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = max_row - min_row min_row = first_unhidden max_row = min_row + sel_len start = self.model().index(min_row, min_col) end = self.model().index(max_row, max_col) selection = QItemSelection() selection.select(start, end) self.setCurrentIndex(start) self.selectionModel().setCurrentIndex( end, QItemSelectionModel.ClearAndSelect) self.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect) if scroll_to_start: self.scrollTo(start) else: self.scrollTo(end)
def keyPressEvent(self, event: QKeyEvent): if event.key() == Qt.Key_Delete: min_row, max_row, start, end = self.selection_range() if min_row == max_row == start == end == -1: return self.setEnabled(False) self.setCursor(Qt.WaitCursor) self.model().delete_range(min_row, max_row, start, end - 1) self.unsetCursor() self.setEnabled(True) self.setFocus() if event.matches(QKeySequence.Copy): self.on_copy_action_triggered() return if event.key() == Qt.Key_Space: min_row, max_row, start, _ = self.selection_range() if start == -1: return self.model().insert_column(start, list(range(min_row, max_row + 1))) if event.key() not in (Qt.Key_Right, Qt.Key_Left, Qt.Key_Up, Qt.Key_Down) \ or event.modifiers() == Qt.ShiftModifier: super().keyPressEvent(event) return min_row, max_row, min_col, max_col = self.selection_range() if min_row == max_row == min_col == max_col == -1: super().keyPressEvent(event) return max_col -= 1 scroll_to_start = True if event.key() == Qt.Key_Right and max_col < self.model().col_count - 1: max_col += 1 min_col += 1 scroll_to_start = False elif event.key() == Qt.Key_Left and min_col > 0: min_col -= 1 max_col -= 1 elif event.key() == Qt.Key_Down and max_row < self.model().row_count - 1: first_unhidden = -1 for row in range(max_row + 1, self.model().row_count): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = max_row - min_row max_row = first_unhidden min_row = max_row - sel_len scroll_to_start = False elif event.key() == Qt.Key_Up and min_row > 0: first_unhidden = -1 for row in range(min_row - 1, -1, -1): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = max_row - min_row min_row = first_unhidden max_row = min_row + sel_len start = self.model().index(min_row, min_col) end = self.model().index(max_row, max_col) selection = QItemSelection() selection.select(start, end) self.setCurrentIndex(start) self.selectionModel().setCurrentIndex(end, QItemSelectionModel.ClearAndSelect) self.selectionModel().select(selection, QItemSelectionModel.ClearAndSelect) if scroll_to_start: self.scrollTo(start) else: self.scrollTo(end)
def keyPressEvent(self, e: QKeyEvent): if e.matches(QKeySequence.Paste): self.load_grid_from_clipboard() else: super().keyPressEvent(e)
def keyPressEvent(self, event: QKeyEvent): if event.key() == Qt.Key_Delete: selected = self.selectionModel().selection() """:type: QtGui.QItemSelection """ if selected.isEmpty(): return min_row = numpy.min([rng.top() for rng in selected]) max_row = numpy.max([rng.bottom() for rng in selected]) start = numpy.min([rng.left() for rng in selected]) end = numpy.max([rng.right() for rng in selected]) self.setEnabled(False) self.setCursor(Qt.WaitCursor) self.model().delete_range(min_row, max_row, start, end) self.unsetCursor() self.setEnabled(True) self.setFocus() if event.matches(QKeySequence.Copy): cells = self.selectedIndexes() cells.sort() currentRow = 0 text = "" for cell in cells: if len(text) > 0 and cell.row() != currentRow: text += "\n" currentRow = cell.row() if cell.data(): text += str(cell.data()) QApplication.instance().clipboard().setText(text) return if event.key() not in (Qt.Key_Right, Qt.Key_Left, Qt.Key_Up, Qt.Key_Down) \ or event.modifiers() == Qt.ShiftModifier: super().keyPressEvent(event) return sel = self.selectionModel().selectedIndexes() cols = [i.column() for i in sel] rows = [i.row() for i in sel] if len(cols) == 0 or len(rows) == 0: super().keyPressEvent(event) return mincol, maxcol = numpy.min(cols), numpy.max(cols) minrow, maxrow = numpy.min(rows), numpy.max(rows) scroll_to_start = True if event.key() == Qt.Key_Right and maxcol < self.model().col_count - 1: maxcol += 1 mincol += 1 scroll_to_start = False elif event.key() == Qt.Key_Left and mincol > 0: mincol -= 1 maxcol -= 1 elif event.key( ) == Qt.Key_Down and maxrow < self.model().row_count - 1: first_unhidden = -1 for row in range(maxrow + 1, self.model().row_count): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = maxrow - minrow maxrow = first_unhidden minrow = maxrow - sel_len scroll_to_start = False elif event.key() == Qt.Key_Up and minrow > 0: first_unhidden = -1 for row in range(minrow - 1, -1, -1): if not self.isRowHidden(row): first_unhidden = row break if first_unhidden != -1: sel_len = maxrow - minrow minrow = first_unhidden maxrow = minrow + sel_len start = self.model().index(minrow, mincol) end = self.model().index(maxrow, maxcol) selctn = QItemSelection() selctn.select(start, end) self.selectionModel().setCurrentIndex( end, QItemSelectionModel.ClearAndSelect) self.selectionModel().select(selctn, QItemSelectionModel.ClearAndSelect) if scroll_to_start: self.scrollTo(start) else: self.scrollTo(end)
def keyPressEvent(self, event: QKeyEvent): if event.type() == QKeyEvent.KeyPress \ and event.matches(QKeySequence.Copy): self.copy_selection_to_clipboard() else: super().keyPressEvent(event)
def keyPressEvent(self, event: QKeyEvent) -> None: if event.matches(QKeySequence.Find): self.find.setFocus() else: super().keyPressEvent(event)