PyQt: How to pass arguments while emitting a signal
This is my OLD blog. I've copied this post over to my NEW blog at:
http://www.saltycrane.com/blog/2008/01/pyqt-how-to-pass-arguments-while/
You should be redirected in 2 seconds.
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_())
1 comment:
Thanks. Struggled with that in the docs for ages.
Post a Comment