PyQt5 快捷方式一直不起作用
PyQt5 Shortcut does not work all the time
所以我用 PyQt5 构建了一个 GUI,并为我的子菜单栏创建了一个快捷方式,如下所示:
''' Menubar '''
mainMenu = self.menuBar()
''' Sub-Menubar '''
fileMenu = mainMenu.addMenu('Options')
Pathfinder = QAction('Choose Folder', self)
Pathfinder.setShortcut("Shift+L")
Pathfinder.triggered.connect(lambda: self.clicked_menu(0))
fileMenu.addAction(Pathfinder)
按 Shift + L 可以正常工作...只要我的鼠标光标在某个元素中未处于活动状态。例如,如果我更改了我的一个 spinBox 中的值,并且光标(=插入符号)在 spinBox 中“闪烁”,则 Shift+L 不执行任何操作。
self.spinBoxMaxHolesPerCycle = QSpinBox(self)
self.spinBoxMaxHolesPerCycle.setGeometry(485, 310, 45, 20)
self.spinBoxMaxHolesPerCycle.setMaximum(200)
self.spinBoxMaxHolesPerCycle.setValue(100)
self.spinBoxMaxHolesPerCycle.valueChanged.connect(lambda: self.changedValue(11))
有没有一种方法可以解决这个问题,而无需单击其他地方来“解除附加”光标?
您遇到的行为与鼠标光标无关,而是由于键盘焦点(您可以设置不过,通过单击它可以将焦点移至小部件)。
问题是 QSpinBox 小部件包含一个 QLineEdit,用于使用键盘编辑值,并且由于您的快捷方式 可能 被解释为文本(大写字母“L " 字母),小部件自动“吃掉”键盘事件,防止它传播到父级。
如果您只关心一个小部件,您可以将它子类化并覆盖它 keyPressEvent
,如果它与快捷方式不匹配,只需调用基本实现。
由于您可能希望将行为应用于多个小部件,因此一种解决方案是在 QApplication 上安装事件过滤器并检查按键事件:如果事件与您的快捷方式匹配,则触发操作并return 正确。
# ...
# make the action an attribute, so that it can be accessed from elsewhere
self.pathfinderAction = QtWidgets.QAction('Choose Folder', self)
# ...
QtWidgets.QApplication.instance().installEventFilter(self)
def eventFilter(self, source, event):
if (isinstance(source, QtWidgets.QWidget) and
event.type() == QtCore.QEvent.KeyPress):
sequence = QtGui.QKeySequence(int(event.modifiers()) + event.key())
if sequence == self.pathfinderAction.shortcut():
# the event matches the shortcut
self.pathfinderAction.trigger()
return True
return super().eventFilter(source, event)
所以我用 PyQt5 构建了一个 GUI,并为我的子菜单栏创建了一个快捷方式,如下所示:
''' Menubar '''
mainMenu = self.menuBar()
''' Sub-Menubar '''
fileMenu = mainMenu.addMenu('Options')
Pathfinder = QAction('Choose Folder', self)
Pathfinder.setShortcut("Shift+L")
Pathfinder.triggered.connect(lambda: self.clicked_menu(0))
fileMenu.addAction(Pathfinder)
按 Shift + L 可以正常工作...只要我的鼠标光标在某个元素中未处于活动状态。例如,如果我更改了我的一个 spinBox 中的值,并且光标(=插入符号)在 spinBox 中“闪烁”,则 Shift+L 不执行任何操作。
self.spinBoxMaxHolesPerCycle = QSpinBox(self)
self.spinBoxMaxHolesPerCycle.setGeometry(485, 310, 45, 20)
self.spinBoxMaxHolesPerCycle.setMaximum(200)
self.spinBoxMaxHolesPerCycle.setValue(100)
self.spinBoxMaxHolesPerCycle.valueChanged.connect(lambda: self.changedValue(11))
有没有一种方法可以解决这个问题,而无需单击其他地方来“解除附加”光标?
您遇到的行为与鼠标光标无关,而是由于键盘焦点(您可以设置不过,通过单击它可以将焦点移至小部件)。
问题是 QSpinBox 小部件包含一个 QLineEdit,用于使用键盘编辑值,并且由于您的快捷方式 可能 被解释为文本(大写字母“L " 字母),小部件自动“吃掉”键盘事件,防止它传播到父级。
如果您只关心一个小部件,您可以将它子类化并覆盖它 keyPressEvent
,如果它与快捷方式不匹配,只需调用基本实现。
由于您可能希望将行为应用于多个小部件,因此一种解决方案是在 QApplication 上安装事件过滤器并检查按键事件:如果事件与您的快捷方式匹配,则触发操作并return 正确。
# ...
# make the action an attribute, so that it can be accessed from elsewhere
self.pathfinderAction = QtWidgets.QAction('Choose Folder', self)
# ...
QtWidgets.QApplication.instance().installEventFilter(self)
def eventFilter(self, source, event):
if (isinstance(source, QtWidgets.QWidget) and
event.type() == QtCore.QEvent.KeyPress):
sequence = QtGui.QKeySequence(int(event.modifiers()) + event.key())
if sequence == self.pathfinderAction.shortcut():
# the event matches the shortcut
self.pathfinderAction.trigger()
return True
return super().eventFilter(source, event)