1

我之前有一个关于 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)
4

1 回答 1

1

使用 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)
于 2019-03-16T17:15:51.220 回答