kirill.login
kirill.login

Reputation: 961

PyQt4 PushButton connection issue

There is class GUI_XMLtool which has been generated from .ui QtDesigner file. And there is a MyApp class. Now I am trying to connect PushButton (XSD_path_PB) click to MyApp method invoke. I am tryng by two ways (one of them is commented)

from PyQt4 import QtCore, QtGui
from GUI import GUI_XMLtool
import sys

class MyApp(QtGui.QMainWindow, GUI_XMLtool):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.ui = GUI_XMLtool()
        self.ui.setupUi(self)

        self.connect(self.ui.XSD_path_PB, QtCore.SIGNAL("clicked()"), self.someMethod())
        #self.ui.XSD_path_PB.clicked.connect(self.someMethod())

    def someMethod(self):
        print "wahwah"

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

This is the easiest thing but I get a terrible error trace:

wahwah
Traceback (most recent call last):
  File "C:/Users/***/PycharmProjects/XMLTool/GUI/Main.py", line 20, in <module>
    window = MyApp()
  File "C:/Users/***/PycharmProjects/XMLTool/GUI/Main.py", line 12, in __init__
    self.connect(self.ui.XSD_path_PB, QtCore.SIGNAL("clicked()"), self.someMethod())
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'

What is wrong?

Upvotes: 0

Views: 165

Answers (1)

Mel
Mel

Reputation: 6075

With the new style signal and slot (the one commented), you should write:

self.ui.XSD_path_PB.clicked.connect(self.someMethod)

instead of

self.ui.XSD_path_PB.clicked.connect(self.someMethod())

When the second line is executed,self.someMethod() is called, and returns a value (in your case the default return value None). Then, connect is called with this value. It expects this value to be a python callable (a method), but it's not (it's None), so you get a TypeError.

To get the python callable, you simply use self.someMethod

>>>type(someMethod)                                                                                                                 
<class 'function'>  

Upvotes: 0

Related Questions