def eventFilter(self, widget, event): if widget == self and event.type() == QtCore.QEvent.ContextMenu: menu = self.createStandardContextMenu() actions = menu.actions() for action in actions: action.setShortcut(QtGui.QKeySequence()) action_string = action.text() pos = action_string.rfind("\t") if pos > 0: action_string = action_string[:pos] action.setText(action_string) action_before = None if len(actions) > 0: action_before = actions[0] clear = QtGui.QAction("Clear Shortcut", menu) menu.insertAction(action_before, clear) menu.insertSeparator(action_before) clear.setEnabled(not self.__key_sequence.isEmpty()) clear.triggered.connect(self.clear_shortcut) menu.exec_(event.globalPos()) del menu event.accept() return True return QtGui.QLineEdit.eventFilter(self, widget, event)
def __init__(self, parent=None): QtGui.QLineEdit.__init__(self, parent) self.__key_sequence = QtGui.QKeySequence() # self.__line_edit.installEventFilter(self) self.setReadOnly(True) self.setAttribute(QtCore.Qt.WA_InputMethodEnabled)
def handle_key_event(self, event): if event.isAutoRepeat(): return key = event.key() if (key == QtCore.Qt.Key_Control or key == QtCore.Qt.Key_Shift or key == QtCore.Qt.Key_Meta or key == QtCore.Qt.Key_Alt or key == QtCore.Qt.Key_Super_L or key == QtCore.Qt.Key_AltGr): return if not event.modifiers(): return key |= self.translate_modifiers(event.modifiers(), event.text()) self.key_sequence = QtGui.QKeySequence(key) self.key_sequence_changed.emit( self.key_sequence, event.nativeModifiers(), event.nativeVirtualKey(), ) event.accept()
def clear_shortcut(self): if self.__key_sequence.isEmpty(): return self.key_sequence = QtGui.QKeySequence() self.key_sequence_changed.emit(self.key_sequence, 0, 0)