PyQT - Signals and Slots?

Veek M vek.m1234 at gmail.com
Mon Oct 10 09:32:58 EDT 2016


I'm reading Rapid GUI Programming - Mark Summerfield with Python and QT 
pg 131. Basically the mechanism is an event table which maps a 'signal' 
to a 'function/slot' -correct?

  self.connect(dial, SIGNAL("valueChanged(int)"), spinbox.setValue)

Here, dial.valueChanged -> spinbox.setValue

 s.connect(w, SIGNAL("signalSignature"), functionName)
 s.connect(w, SIGNAL("signalSignature"), instance.methodName)
 s.connect(w, SIGNAL("signalSignature"), instance, 
SLOT("slotSignature"))

Here, w.signalSignature -> functionName
                        -> instance.methodName
                        -> instance.slotSignature

If signalSignature is a C++ implemented thingy then we got to pass type 
info as part of the mapping so "signalSignature(int, float, const char 
*). PyQT signals are any type and any number of args..


If the Slot-function is implemented in C++ it's better to use the SLOT() 
mechanism:
 self.connect(dial, SIGNAL("valueChanged(int)"), spinbox, 
SLOT("setValue(int)"))
Here, we are mapping dial.valueChanged(int) --> spinbox.setValue(int)

----
The part i found tricky was this:

class ZeroSpinBox(QSpinBox):
    zeros = 0
    def __init__(self, parent=None):
        super(ZeroSpinBox, self).__init__(parent)
        self.connect(self, SIGNAL("valueChanged(int)"), self.checkzero)

    def checkzero(self):
        if self.value() == 0:
            self.zeros += 1
            self.emit(SIGNAL("atzero"), self.zeros)

ZeroSpinBox.valueChanged -> ZeroSpinBox.checkzero? Why is he mapping 
back to himself? Shouldn't it be widget-to-widget?

Then he raises a signal 'atzero' with one arg - the lack of a atzero() 
implies it's a 'short-circuit' signal so self.zeroes is a python data 
type. And in the book he says:

#######################
Here is how we connect to the signal in the form’s __init__() method:
zerospinbox = ZeroSpinBox()
...
self.connect(zerospinbox, SIGNAL("atzero"), self.announce)
Again, we must not use parentheses because it is a short-circuit signal. 
And
for completeness, here is the slot it connects to in the form:
def announce(self, zeros):
    print "ZeroSpinBox has been at zero %d times" % zeros
###########################

Whaaa...t?? Could someone explain what exactly is his grand design 
besides being awfully circuitous? So he has some other Form thingy.. and 
in that he sets up another mapping from ZeroSpinBox.atzero --> 
ZeroSpinBox.announce ???? where's announce and atzero defined?








More information about the Python-list mailing list