PyQt5在打开的菜单中使用快捷方式

问题描述

在PyQt5应用程序中,我有一个QMenu。我要使菜单打开后,用户可以使用键1、2、3等在菜单中选择选项1、2、3等。但是,我在设置快捷方式或让快捷方式对按键做出反应时遇到麻烦。

我已经从this网站上举了一个例子,并对其进行了一些微调以显示我的问题。我尝试在addAction函数中分配快捷方式,但这不起作用。

我尝试创建常规的QShortcut,但是当菜单打开时,它们不再响应。我注意到我可以使用向上和向下箭头更改所选的选项,然后使用Enter键确认选择,因此QMenu可以捕获按键。但是如何分配自己的快捷方式?

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication,QMainWindow,QMenu
import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 Context Menu"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300
        self.Initwindow()


    def Initwindow(self):
        self.setwindowIcon(QtGui.QIcon("icon.png"))
        self.setwindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.show()

    def contextMenuEvent(self,event):
        contextMenu = QMenu(self)
        newAct = contextMenu.addAction("New",self.triggered,shortcut='A')
        openAct = contextMenu.addAction("Open",self.triggered2,shortcut='B')
        quitact = contextMenu.addAction("Quit",self.triggered3,shortcut='C')
        action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        if action == quitact:
            self.close()

    def triggered(self):
        print("triggered 1")

    def triggered2(self):
        print("triggered 2")

    def triggered3(self):
        print("triggered 3")
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

解决方法

打开QMenu时,快捷方式不起作用,但是accelerator keys在起作用。您用P声明它们:

scatterv

弹出上下文菜单时,按&将触发正确的功能。

,

QKeySequence class封装了快捷方式所使用的键序列。 更多... https://doc.qt.io/qt-5/qkeysequence.html

尝试一下:

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication,QMainWindow,QMenu


class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 Context Menu"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300
        self.InitWindow()

    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.show()

    def contextMenuEvent(self,event):
        contextMenu = QMenu(self)
#        newAct = contextMenu.addAction("New",self.triggered,shortcut='A')
        newAct = contextMenu.addAction(
            "New (&A)",shortcut=QtGui.QKeySequence.New)            # +++
        openAct = contextMenu.addAction(
            "Open (&B)",self.triggered2,shortcut=QtGui.QKeySequence.Open)         # +++
        quitAct = contextMenu.addAction(
            "Quit (&C)",self.triggered3,shortcut=QtGui.QKeySequence.Quit)         # +++
        action = contextMenu.exec_(self.mapToGlobal(event.pos()))
        if action == quitAct:
            self.close()

    def triggered(self):
        print("triggered 1")

    def triggered2(self):
        print("triggered 2")

    def triggered3(self):
        print("triggered 3")
        
        
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

enter image description here