PyQT - Signals and Slots?

Mark Summerfield list at qtrac.plus.com
Mon Oct 10 10:49:35 EDT 2016


The ZeroSpinBox is a tiny example designed to show how the signal/slot
mechanism works. It is just a QSpinBox with the addition of remembering
how many times (all the) ZeroSpinBox(es) have had a 0 value.

Nowadays the connections would be made with a new improved syntax:

self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero) # OLD
self.valueChanged.connect(self.checkzero) # NEW
# or
self.valueChanged[int].connect(self.checkzero) # NEW

Similarly the emit:
self.emit(SIGNAL("atzero"), self.zeros) # OLD
self.atzero.emit(self.zeros) # NEW

What's happening inside ZeroSpinBox? Whenever its value is set to 0 it
calls its own checkzero() method, and this in turn emits an atzero signal
with the number of zeros so far. Why does it do this? Just to show the
mechanism. It doesn't matter whether you connect a widget to itself
(unusual but done here), or to another widget (the norm).

See the book's website https://www.qtrac.eu/pyqtbook.html
for all the source code including some updated with the new syntax.
(But the book is old, so even the Python 3.1 examples aren't as Pythonic as they would be written in Python 3.4+.)



More information about the Python-list mailing list