[PyQt] A simple question about signals and slots

Adam W. awasilenko at gmail.com
Sat Sep 5 00:53:17 BST 2009


I keep forgetting to CC the newsgroup...

Anyway, I think you're still melting the two terms together.

> what I'm asking is if there's a mechanism in pyqt to emit an event in B

What you want to do is connect the signal from B to slot C, and signal
C to slot B in the namespace of A, not in the namespace of B and C.
This is the way its done in all the examples in Summerfield's book.
The only event you need to worry about is if there is no signal for
move (which I don't think there is), then you need to use an event
handler to make your own signal.

> which can be detected in C....

Signals are not "detected".  After you connect a signal to a slot, and
said signal is emitted, the slot immediately gets called.  There is no
listening needed.

I hope that makes some sort of sense.  If not I can try to rephrase it.

- Adam

On Fri, Sep 4, 2009 at 7:32 PM, William<abecedarian314159 at yahoo.com> wrote:
> You are correct, what I want is an event.  For example, the user does an
> action in one window (for example, moving a rectangle around) and when they
> are done, it emits an event
> (for example,
> self.emit(SIGNAL("rectangleMoved"),'moved')
> )
> , which another window can listen for and when it detects the event, can act
> (for example, update a plot).  I can do this if the window is higher in the
> heirarchy for example if window A in my diagram acts upon an event generated
> by B, what I don't know is a clean way for C to receive events generated by
> A.  I could directly connect things in B and C, but tight coupling seems
> likely to lead to less maintainable code.  I could  let the event pass up to
> A and then have A connect down to C, but having "god" classes also seems
> like a step away from the road to maintainability.  So, what I'm asking is
> if there's a mechanism in pyqt to emit an event in B, which can be detected
> in C....
> Thanks,
> William
> ________________________________
> From: Adam W. <awasilenko at gmail.com>
> To: William <abecedarian314159 at yahoo.com>
> Sent: Friday, September 4, 2009 7:17:35 PM
> Subject: Re: [PyQt] A simple question about signals and slots
>
> If B and C are under the same namespace as A, you can connect Signals
> with one of these:
> s.connect(w, SIGNAL("signalSignature"), functionName)
> s.connect(w, SIGNAL("signalSignature"), instance.methodName)
> s.connect(w, SIGNAL("signalSignature"), instance, SLOT("slotSignature"))
> where s and w are QObjects with s usually being self (in your example
> the A object), and w the object whose signal you want to connect.
>
> But I think you're confusing the signals and slots with events and
> event handlers.
>
> Paraphrasing from Mark Summerfield's book: Qt has two communication
> mechanisms: a low-level event-handling mechanism which is similar to
> those provided by all other GUI libraries, and a high-level mechanism
> which Trolltech have coined "signals and slots".  Every QObject
> supports some predefined signals/slots.  You can create your own
> signals and slots too.  If you need an even that is not available as a
> signal, like capturing a keypress, then you need to use events.
>
> If a signal is not connected to a slot of function, thats the end of
> the road for it.  However its unhandled events that climb the parent
> latter till it eventually hits top window and is discarded.
>
> Hope that helps,
> - Adam
>
> On Fri, Sep 4, 2009 at 6:38 PM, William<abecedarian314159 at yahoo.com> wrote:
>> Hi!  I have what I hope is a simple question about signals and slots.  As
>> I
>> understand it,
>> if events aren't acted upon, then they propogate upwards.  But suppose you
>> have something like this:
>>     A
>>  /     \
>> B     C
>>
>> Where B and C are children of A.  How does B send a signal to C?  I'd like
>> to keep B and C decoupled
>> (for example, B and C could be dockwidgets, or even other main windows).
>> Is there something like wx.py.dispatcher?
>>
>> Thanks,
>> William
>>
>> _______________________________________________
>> PyQt mailing list    PyQt at riverbankcomputing.com
>> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>



More information about the PyQt mailing list