I am writing my first Python app with PyQt4. I have a MainWindow and a Dialog class, which is a part of MainWindow class:
self.loginDialog = LoginDialog();
I use slots and signals. Here's a connection made in MainWindow:
QtCore.QObject.connect(self.loginDialog, QtCore.SIGNAL("aa(str)"), self.login)
And I try to emit signal inside the Dialog class (I'm sure it is emitted):
self.emit(QtCore.SIGNAL("aa"), "jacek")
Unfortunately, slot is not invoked. I tried with no arguments as well, different styles of emitting signal. No errors, no warnings in the code. What might be the problem?
You don't use the same signal, when emitting and connecting.
QtCore.SIGNAL("aa(str)")
is not the same asQtCore.SIGNAL("aa")
. Signals must have the same signature. By the way, if you are defining your own signals, don't define parametres. Just write SIGNAL('aa'), because defining parametres is a thing from C++ and Python version of Qt doesn't need this.So it should look like this:
and if you pass any parametres in emit, your login method must accept those parametres. Check, if this helps :-)
There are some concepts to be clarified
[QT signal & slot] VS [Python signal & slot]
All the predefined signals & slots provided by pyqt are implemented by QT's c++ code. Whenever you want to have a customized signal & slot in Python, it is a python signal & slot. Hence there are four cases to emits a signal to a slot:
The code below shows how to connect for these four different scnarios
Conclusion is --
Signal signature for Python signal differentiate from that of QT signal in that it doesn't have the parenthesis and can be passed any python data types when you emit it. The Python signal is created when you emit it.
For slot, there are three forms of signatures.
Number 1 & 2 are available for Python slot, while number 2 & 3 are available for QT slot. It is clear that besides QT predefined slot, any python callable function/methods is qulified to be a Python slot.
These points are made in Summerfield's article on Signals and Slots.
[Old style qt signal & slot] VS [new style qt singal & slot]
Well, all the description above is based on the old style pyqt signal & slot. As @Idan K suggested there is an alternative new-style to do the things, especially for the Python signal. Refer to here for more.
I haven't used PyQT4 but take a look at here.
What @bialix suggested should have worked, but try an alternative way of connecting:
For a more detailed explanation of how signals/slots work in PyQt I'd suggest going through it's documentation, specifically this section.
Looks like you miss the "SLOT" part in your connect call.
Here is an example :
then
Hope this helps !
I checked your code and it looks like the problem is in the way how you're connecting your signal
you emit the signal in Ui_Dialog class
self.emit(QtCore.SIGNAL("aa()"))
you connect to the signal in Ui_MainWindow's setupUi method by calling
QtCore.QObject.connect(self.loginDialog.ui, QtCore.SIGNAL("aa()"), self.login)
notice first parameter is changed to self.loginDialog.ui; your original connect call was using self.loginDialog which is of the LoginDialog type, whereas signal is emitted by the Ui_Dialog class which is ui property of the LoginDialog. After this change login method of the Ui_MainWindow got called
hope this helps, regards