扩展上下文菜单中的Qaction快捷方式未被触发

0 人关注

我试图在 QLineEdit 的上下文菜单中增加一个用于替换文本的条目。我可以用 .createStandardContextMenu() 扩展上下文菜单,这样做很好。但当我试图用 .setShortcut(QKeySequence(Qt.CTRL + Qt.Key_R)) 添加一个快捷方式时,它不会对该键作出反应。不同的键也一样,我试过了。此外,用 QAction('&Replace', self) 制作的快捷方式也不工作。 在SO和其他资料中的一些例子都是以同样的方式构建的,所以我想知道是否没有其他人遇到同样的问题。似乎我错过了什么。但是什么呢?我想不出来,检查了 docs multiple times.

工作实例。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class ECM(QWidget):
    def __init__(self):
        super(ECM, self).__init__()
        self.setWindowTitle("Extended Context Menu")
        self.lineEdit = QLineEdit()
        self.lineEdit.setContextMenuPolicy(Qt.CustomContextMenu)                            
        self.lineEdit.customContextMenuRequested.connect(self.my_contextMenuEvent)
        layout = QVBoxLayout()
        layout.addWidget(self.lineEdit)
        self.setLayout(layout)
        self.setFixedSize(800,200)
        self.show()
    def replace(self):
        print("replace")
    def my_contextMenuEvent(self):                                           
        print("my_contextMenuEvent")                                         
        menu = self.lineEdit.createStandardContextMenu()
        action = QAction('&Replace', self)
        action.setStatusTip('Replace values')
        action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_R))
        action.triggered.connect(self.replace)
        menu.addAction(action)                                               
        menu.exec_()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    sender = ECM()
    app.exec_()
    
1 个评论
如果你用 addAction() 将动作添加到小组件中,键盘快捷键就会起作用(这意味着你不应该每次都创建新的动作,而是重复使用当前的动作)。
python
pyqt5
qmenu
qaction
Papageno
Papageno
发布于 2021-04-17
1 个回答
Papageno
Papageno
发布于 2021-04-17
已采纳
0 人赞同

基于 琴师 的评论,我得出了以下解决方案。

从文件中摘录。

  • If you want to extend the standard context menu, reimplement this function, call createStandardContextMenu() and extend the menu returned .
  • The default use of the QAction list (as returned by actions()) is to create a context QMenu.
  • 对我来说,这并不完全符合逻辑,这不是第一次;-)

    最终代码。

    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    import sys
    class ECM(QWidget):
        def __init__(self):
            super(ECM, self).__init__()
            self.setWindowTitle("Extended Context Menu")
            self.lineEdit = QLineEdit()
            self.lineEdit.setContextMenuPolicy(Qt.CustomContextMenu)                            
            self.lineEdit.customContextMenuRequested.connect(self.my_contextMenuEvent)
            layout = QVBoxLayout()
            layout.addWidget(self.lineEdit)
            self.setLayout(layout)
            self.setFixedSize(800,200)
            action = QAction('&Replace', self)
            action.setStatusTip('Replace values')
            action.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_R))
            action.triggered.connect(self.replace)
            self.lineEdit.addAction(action)
            self.show()
        def replace(self):
            print("replace")
        def my_contextMenuEvent(self):                                           
            menu = self.lineEdit.createStandardContextMenu()
            menu.addActions(self.lineEdit.actions())
            menu.exec_()