[PyQt] A simple question about signals and slots
William
abecedarian314159 at yahoo.com
Sat Sep 5 01:42:59 BST 2009
Adam and Phil, thanks for your time. I think I almost understand--but, I still have some confusion.
Adam, if I follow your suggestion, then parents must really know their children well in order for signalling to work. Which can be the case sometimes. But, what I would like is:
B generates a custom event (rather than a signal). Upon that event, C can take an action. I think it would be cleaner if neither B nor C required the mediation of A. Why do this? Suppose A has 15 distinct children that need to be triggered upon a signal from B? Or, suppose the heirarchy changes so that instead we have:
A
/ \
B D
/ \
C E
\
F
Now, if a method in C still needs to be triggered based on something that happens in B, then code in A has to be rewritten, along with new code added to D. Suppose that I create a new window F that also needs to be triggered by something in B?
Whereas the essential relationship (communication wise) is still fundamentally between B and C.
So, it sounds like the signal slot pattern is not the way I want to go, because unless I directly connect B and C then the communication pathway is fragile.
But, if I generate an EVENT (sorry for the language sloppiness earlier) B, it will go up to A and disappear, unless I propagate it down to the leaves (which could be dangerous). So, the question is, whether or not there's a common bus/dispatcher in which B can put some information and whoever wants to be triggered by it, can?
________________________________
From: Adam W. <awasilenko at gmail.com>
Cc: pyqt at riverbankcomputing.com
Sent: Friday, September 4, 2009 7:53:17 PM
Subject: Re: [PyQt] A simple question about signals and slots
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
>>
>
>
_______________________________________________
PyQt mailing list PyQt at riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090905/64ab5e56/attachment-0001.html
More information about the PyQt
mailing list