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

发布于 2024-10-17 11:12:52 字数 4398 浏览 1 评论 0原文

我在使用 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')

I have problems with connect() from PyQt4. For example here is .UI converted via pyuic4.

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_())

And .PY file listing. Please note double-commented line. There is error, but theoretically it should work.

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_())

What I've got is:

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'

Any help would be appreciated.


Many thanks to Luke Woodward, here is a right variant.

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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-10-24 11:12:52

错误出现在双注释行上:

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

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

您所编写的内容与 setText 方法将始终返回 None 具有相同的效果

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

,因为没有任何可返回的内容。出现错误是因为 PyQt 找不到合适的 connect 方法来调用,参数 3 设置为 None - 事实上,错误消息中的最后三行都是提到参数 3 和 NoneType 的问题 - None 的“类型”。

您可以做的是将代码放入方法中,例如:

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

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

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

请注意,因为没有 () 在方法名称后面,我们不是在调用该方法。我们将方法本身传递给self.connect

The error is on the double-commented line:

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

It looks like you want the text of the label set to ABC whenever the button is clicked. However, the above line of code will not achieve this. The problem is that the third argument, self.label.setText('ABC'), is evaluated when the call to self.connect is made, not when the signal fires.

What you've written has the same effect as

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

The setText method will always return None, since there's nothing to return. The error you get arises because PyQt can't find a suitable connect method to call with the argument 3 set to None - indeed, the last three lines in your error message all mention a problem with argument 3 and NoneType - the 'type' of None.

What you could do instead is to put your code in a method, for example:

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

Then, you use this method as argument 3 in the call to self.connect:

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

Note that because there is no () after the method name, we're not calling the method. We're passing the method itself to self.connect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文