PyQt - > connect() - > TypeError:参数与任何重载调用都不匹配

asv*_*asv 5 python pyqt connect

我有来自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_())
Run Code Online (Sandbox Code Playgroud)

和.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_())
Run Code Online (Sandbox Code Playgroud)

我得到的是:

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'
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.


非常感谢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')
Run Code Online (Sandbox Code Playgroud)

Luk*_*ard 11

错误在双注释行上:

##        self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.label.setText('ABC'))
Run Code Online (Sandbox Code Playgroud)

看起来您希望ABC每次单击按钮时都设置标签的文本.但是,上面的代码行不会实现这一点.问题是第三个参数self.label.setText('ABC')是在进行调用时进行评估self.connect,而不是在信号触发时进行评估.

你所写的内容与之相同

value = self.label.setText('ABC')
self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), value)
Run Code Online (Sandbox Code Playgroud)

setText方法将始终返回None,因为没有任何东西可以返回.你得到的错误是因为PyQt无法找到一个合适的connect方法来调用参数3设置为None- 实际上,你的错误信息中的最后三行都提到了参数3的问题和NoneType- '类型' None.

你可以做的是将你的代码放在一个方法中,例如:

def setLabelText(self):
    self.label.setText('ABC')
Run Code Online (Sandbox Code Playgroud)

然后,在调用中使用此方法作为参数3 self.connect:

self.connect(self.btnClearText, QtCore.SIGNAL('clicked()'), self.setLabelText)
Run Code Online (Sandbox Code Playgroud)

请注意,因为()方法名称之后没有,所以我们不会调用该方法.我们将方法本身传递给self.connect.