PyQt: How to pass arguments while emitting a signal
I often forget how to do this so I'm documenting it here for future reference. If I want to emit a signal and also pass an argument with that signal, I can use the form self.emit(SIGNAL("mySignalName"), myarg)
. I connect the signal to a method in the usual way. To use the argument, I merely need to specify the argument in the method definition. What often confuses me is that I don't need to specify arguments in the connect
statement. The example below emits a signal didSomething
and passes two arguments, "important"
and "information"
to the update_label
method.
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
####################################################################
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.label = QLabel(" ")
layout = QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
self.connect(self, SIGNAL("didSomething"),
self.update_label)
self.do_something()
def do_something(self):
self.emit(SIGNAL("didSomething"), "important", "information")
def update_label(self, value1, value2):
self.label.setText(value1 + " " + value2)
####################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
Related posts
- PyQt4 QItemDelegate example with QListView and QAbstractListModel — posted 2008-01-23
- How to install pyqt4 on ubuntu linux — posted 2008-01-15
- Python PyQt Tab Completion example — posted 2008-01-04
- How to capture the Tab key press event with PyQt 4.3 — posted 2008-01-03
- PyQt 4.3 Simple QAbstractListModel/ QlistView example — posted 2008-01-03
Comments
Thanks. Struggled with that in the docs for ages.
Thanks for the tip, your blog is great, i really do benefit a lot of. Your examples are easy to understand and easy to follow.
Thanks, finally an easy to understand post about user defined signals....not easy to find documentation for non informaticians ;)
Thanks! Finally I found understandable example!
Thanks very much for providing this easy-to-understand example. You've really helped me a great deal.
Hi, i know this post is old, but i'm struggling with this problem and your solution help but in this example you are manually emitting the signal, how can i add arguments so signals emitted by the user interaction, or how can i read who emitted the signal? thank you very much
For completness sake(this site is high on google for this problem) and i hope the author do not mind, i'll leave here the link to a another solution.(look for the answer to the question)
Thanks!!! It helps a lots~
This example was very useful.
Changing the call of self.doSomething()
to
QTimer.singleShot(3000, self.do_something)
will make the custom emit() more dramatic
Should it work with PySide too ? Because when the last line ( self.do_something() is commented it brings up the window itself, when not it does nothing.
disqus:2159244016