PyQt --> connect() --> 类型错误:参数与任何重载调用不匹配

5 投票
1 回答
26067 浏览
提问于 2025-04-16 11:48

我在使用PyQt4的connect()时遇到了一些问题。比如,这里是通过pyuic4转换过来的.UI文件。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(382, 258)
        self.btnClearText = QtGui.QPushButton(Dialog)
        self.btnClearText.setGeometry(QtCore.QRect(80, 220, 75, 23))
        self.btnClearText.setObjectName(_fromUtf8("btnClearText"))
        self.btnSetText = QtGui.QPushButton(Dialog)
        self.btnSetText.setGeometry(QtCore.QRect(220, 220, 75, 23))
        self.btnSetText.setObjectName(_fromUtf8("btnSetText"))
        self.textEdit = QtGui.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(10, 20, 361, 41))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.textEdit_2 = QtGui.QTextEdit(Dialog)
        self.textEdit_2.setGeometry(QtCore.QRect(10, 80, 361, 41))
        self.textEdit_2.setObjectName(_fromUtf8("textEdit_2"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(160, 170, 46, 13))
        self.label.setObjectName(_fromUtf8("label"))

        self.retranslateUi(Dialog)
        QtCore.QObject.connect(self.btnSetText, QtCore.SIGNAL(_fromUtf8("released()")), self.textEdit_2.paste)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.btnClearText.setText(QtGui.QApplication.translate("Dialog", "Copy", None, QtGui.QApplication.UnicodeUTF8))
        self.btnSetText.setText(QtGui.QApplication.translate("Dialog", "Paste", None, QtGui.QApplication.UnicodeUTF8))
        self.textEdit.setHtml(QtGui.QApplication.translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">ABC</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

还有这个.PY文件的内容。请注意那行被双重注释的代码。虽然有错误,但理论上它应该能正常工作。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from simple import *

class MyApp(QtGui.QMainWindow, Ui_Dialog):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_Dialog.__init__(self)
        self.setupUi(self)
##        self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.label.setText('ABC'))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv) 
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我遇到的情况是:

TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'

任何帮助都非常感谢。


非常感谢Luke Woodward,这里有一个正确的版本。

class MyApp(QtGui.QMainWindow, Ui_Dialog):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_Dialog.__init__(self)
        self.setupUi(self)
        self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.setLabelText)
    def setLabelText(self):
        self.label.setText('ABC')

1 个回答

10

错误出现在双斜杠注释的那一行:

##        self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.label.setText('ABC'))

看起来你想在每次点击按钮时,把标签的文字设置为 ABC。但是,上面的代码并不能实现这个功能。问题在于第三个参数 self.label.setText('ABC') 是在调用 self.connect 时就被计算了,而不是在信号触发的时候。

你写的代码效果和下面这段是一样的:

value = self.label.setText('ABC')
self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), value)

setText 方法总是返回 None,因为它没有什么可以返回的。你遇到的错误是因为 PyQt 找不到合适的 connect 方法来处理第三个参数为 None 的情况——实际上,你错误信息中的最后三行都提到了第三个参数和 NoneType 的问题,None 的“类型”。

你可以考虑把你的代码放到一个方法里,比如:

def setLabelText(self):
    self.label.setText('ABC')

然后,你可以在调用 self.connect 时,把这个方法作为第三个参数:

self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.setLabelText)

注意,因为方法名后面没有 (),所以我们并没有调用这个方法。我们是把 方法本身 传给 self.connect

撰写回答