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)