2011-02-14 17 views
5

Tengo problemas con connect() desde PyQt4. Por ejemplo, aquí está .UI convertido a través de pyuic4.PyQt -> connect() -> TypeError: los argumentos no coinciden con ninguna llamada sobrecargada

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

Y .PY lista de archivos. Tenga en cuenta la línea comentada doble. Hay un error, pero teóricamente debería funcionar.

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

Lo que tengo es:

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' 

Cualquier ayuda sería muy apreciada.


Muchas gracias a Luke Woodward, aquí hay una variante correcta.

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

Respuesta

11

El error está en la línea de doble comentado:

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

Parece que desea que el texto de la etiqueta de conjunto de ABC cada vez que se hace clic en el botón. Sin embargo, la línea de código anterior no logrará esto. El problema es que el tercer argumento, self.label.setText('ABC'), se evalúa cuando se realiza la llamada a self.connect, no cuando se dispara la señal.

lo que has escrito tiene el mismo efecto que

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

El método setText siempre devolverá None, ya que no hay nada que devolver. El error que obtienes surge porque PyQt no puede encontrar un método adecuado connect para llamar con el argumento 3 establecido en None - de hecho, las últimas tres líneas en tu mensaje de error mencionan un problema con el argumento 3 y NoneType - el 'tipo' de None.

Lo que podría hacer en su lugar es poner su código en un método, por ejemplo:

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

A continuación, utiliza este método como argumento 3 en la llamada a self.connect:

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

Tenga en cuenta que debido a que no hay () después del nombre del método, no estamos llamando al método. Estamos pasando el método en sí a self.connect.

+1

También tenga en cuenta que con el nuevo estilo de PyQt Signals and Slots (http://www.riverbankcomputing.com/static/Docs/PyQt4/html/new_style_signals_slots.html) se puede conectar una señal y ranura con un código menor. Por lo tanto, la señal de creación de códigos y las ranuras anteriores se pueden escribir como: self.btnClearText.clicked.connect (self.setLabelText). – sateesh

+0

@sateesh Gracias por el enlace - No sabía de eso. –

+0

¡Muchas gracias! He publicado la solución en el segundo mensaje. – asv

Cuestiones relacionadas