我之前有一个关于QKeySequence here的问题。它起作用了,但是当我将其应用于代码时,当按钮单击事件在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)
答案 0 :(得分:1)
使用PyQt5进行相同的测试(进行一些兼容性更改)可以正常工作,因此我认为这是PySide2错误。一种解决方法是使用lambda
或functools.partial
来传递密钥。
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)