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
7
Comments
—
Comments feed for this post
#2 Fatih Arslan commented on 2008-09-18:
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.
#3 Peter Schmidtke commented on 2010-09-14:
Thanks, finally an easy to understand post about user defined signals....not easy to find documentation for non informaticians ;)
#5 Danny commented on 2011-03-14:
Thanks very much for providing this easy-to-understand example. You've really helped me a great deal.
#6 guillermo commented on 2011-03-19:
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
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(6)
-
android
(2)
-
aws
(10)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(15)
-
datetime
(4)
-
decorators
(4)
-
django
(41)
-
emacs
(22)
-
files_directories
(12)
-
git
(6)
-
hardware
(6)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(6)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(6)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(157)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(12)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(66)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(8)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 Anonymous commented on 2008-02-22:
Thanks. Struggled with that in the docs for ages.