我在这里有一个关于Qkey序列的问题。它有效,但是当我将其应用于我的代码时,当按钮点击事件在QkeySequence Line之前进行时,Qkey序列出现后似乎是错误的。
注意: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)
单击BTNDSR后仅键入1和2个作品,输入3不起作用。这意味着数字3不是在打印出来,而是单击时数字1和2。按3:
时返回此错误消息sequence = shorcut.key()
AttributeError: 'NoneType' object has no attribute 'key'
如果它很重要,我还在此处附上我的基本代码:
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错误。解决方法是使用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)