PySide2 | Finding out which QKeySequence was press

2019-08-09 17:19发布

问题:

I had a previous question about QKeySequence here. It worked but when I applied it to my code there seemed to be an error when the QKeySequence comes after line when the button click event goes before the QKeySequence line.

Note: The GUI consists of only two buttons: self.btnDSR and self.btnOther.

Taking from the answer of eyllanesc in the previous question, my code follows:

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)

Typing only 1 and 2 works, typing 3 doesn't work after clicking the btnDSR. Meaning the number 3 is not printing out but number 1 and 2 does when clicked. This error message is returned when pressing 3:

sequence = shorcut.key()
AttributeError: 'NoneType' object has no attribute 'key'

In case it is relevant, I am also attaching my basic code for testing GUIs here:

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)

回答1:

Testing the same with PyQt5 (making some compatibility changes) works correctly so I think it's a PySide2 bug. A workaround is to use a lambda or functools.partial to pass the key.

1. lambda:

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)