PySide2 |找出按下了哪个 QKeySequence 2

PySide2 | Finding out which QKeySequence was pressed 2

我之前有一个关于 QKeySequence 的问题 。它有效,但是当我将它应用到我的代码时,当按钮单击事件在 QKeySequence 行之前发生时,QKeySequence 出现在行之后似乎出现错误。

注意:GUI 仅包含两个按钮:self.btnDSR 和 self.btnOther。

根据上一题eyllanesc的回答,我的代码如下:

class MainWindow(QtWidgets.QMainWindow, test_mainWindow.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.signals()

    @QtCore.Slot()
    def test_func(self):
        shorcut = self.sender()
        sequence = shorcut.key()
        print(sequence.toString())

    def btn_clicked(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("3"), self, activated=self.test_func)
        print('Shortcut 3 now works!')  # But it doesn't

    def signals(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("1"), self, activated=self.test_func)
        QtWidgets.QShortcut(QtGui.QKeySequence("2"), self, activated=self.test_func)
        QtCore.QObject.connect(self.btnDSR, QtCore.SIGNAL('clicked()'), self.btn_clicked)
        QtCore.QObject.connect(self.btnOther, QtCore.SIGNAL('clicked()'), self.close)

仅键入 1 和 2 有效,单击 btnDSR 后键入 3 无效。这意味着数字 3 不会打印出来,但点击时会打印出数字 1 和 2。按 3:

时返回此错误消息
sequence = shorcut.key()
AttributeError: 'NoneType' object has no attribute 'key'

如果相关,我还在这里附上我用于测试 GUI 的基本代码:

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(440, 418)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btnDSR = QtWidgets.QPushButton(self.centralwidget)
        self.btnDSR.setGeometry(QtCore.QRect(120, 110, 93, 28))
        self.btnDSR.setObjectName("btnDSR")
        self.btnOther = QtWidgets.QPushButton(self.centralwidget)
        self.btnOther.setGeometry(QtCore.QRect(150, 180, 141, 28))
        self.btnOther.setObjectName("btnOther")
        MainWindow.setCentralWidget(self.centralwidget)
        self.btnDSR.setText(QtWidgets.QApplication.translate("MainWindow", "DSR Button", None, -1))
        self.btnOther.setText(QtWidgets.QApplication.translate("MainWindow", "Other Button", None, -1))
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

使用 PyQt5 进行相同测试(进行一些兼容性更改)工作正常,因此我认为这是一个 PySide2 错误。解决方法是使用 lambdafunctools.partial 来传递密钥。

1.拉姆达:

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.signals()

    @QtCore.Slot(str)
    def test_func(self, key):
        print(key)

    def btn_clicked(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("3"), self, activated=  lambda key="3": self.test_func(key))
        print('Shortcut 3 now works!')  # But it doesn't

    def signals(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("1"), self, activated=  lambda key="1": self.test_func(key))
        QtWidgets.QShortcut(QtGui.QKeySequence("2"), self, activated=  lambda key="2": self.test_func(key))
        QtCore.QObject.connect(self.btnDSR, QtCore.SIGNAL('clicked()'), self.btn_clicked)
        QtCore.QObject.connect(self.btnOther, QtCore.SIGNAL('clicked()'), self.close)

2。 functools.partial

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.signals()

    @QtCore.Slot(str)
    def test_func(self, key):
        print(key)

    def btn_clicked(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("3"), self, activated= partial(self.test_func, "3"))
        print('Shortcut 3 now works!')  # But it doesn't

    def signals(self):
        QtWidgets.QShortcut(QtGui.QKeySequence("1"), self, activated= partial(self.test_func, "1"))
        QtWidgets.QShortcut(QtGui.QKeySequence("2"), self, activated= partial(self.test_func, "2"))
        QtCore.QObject.connect(self.btnDSR, QtCore.SIGNAL('clicked()'), self.btn_clicked)
        QtCore.QObject.connect(self.btnOther, QtCore.SIGNAL('clicked()'), self.close)