tags:

views:

144

answers:

3

I am a total newbie to Qt. As I was reading the documentation, I came across this configuration:

connect( Object1, Signal1, Object2, slot1 )
connect( Object1, Signal1, Object2, slot2 )

What could possibly be the use-case for this?

Looks odd to me coming from an Erlang/Python background. It must have to do with C++ inheritance twists and turns I guess.

+2  A: 

This is for cases when you have something like one button that changes two parts of another. It may sound silly, but it would be equivalent to calling the second slot function from the first slot.

Say, clicking the play/pause button makes the stop button active or in active and also changes the tool tip. This could easily be done with one slot, but you may want the option to do them independently other times. To promote reuse, you use the above method of connecting one signal to 2 slots.

Adam W
Thanks Adam. Looks silly to me though: I would promote an abstraction where the emitter of the signal doesn't need to care what needs to happen on the sink side.
jldupont
@jldupont : The emitter of the signal doesn't have to care about what needs to happen on the sink side. It just emits the signal and goes on with its task. The one making the connection doen't have to be the emitter, a connection can very well be made by the sink, or even by a third party (if the signal and the slots are public).
Fred
To continue with what Fred said, I almost always find it the case that the sink makes the connection (directly or through a proxy). Having a source that knows all the sinks directly somewhat defeats the purpose. And as Fred said, remember that whatever emits the signal doesn't care who receives it. However it may run the slot code before it continues depending on the connection type and threading.
Adam W
Continuing on Fred and Adam, my experience is that the connections almost always are made by a third party, i.e. the parent of both. Either, in a visual case, the c'tor of the outer window, or, when it comes to singletons and other classes managing application states, the main function.
e8johan
+1  A: 

It would allow other objects to trigger slot1 and slot2 separately.

connect( Object1, Signal1, Object2, slot1 );
connect( Object1, Signal1, Object2, slot2 );
connect( Object3, Signal1, Object2, slot1 );
connect( Object4, Signal1, Object2, slot2 );
William
A: 

Hey,

It's actually a really powerful way of doing MVC... Let's say you want to "connect" views to listen to an object than emit datas...

You can connect a PieChart view with :

connect( MySourceModel, SIGNAL(newDataAreThere()), PieChartView, SLOT(notifyNewDataReceived()));

Later in your application, another module is created and need as well to listen to data... No problem :

connect( MySourceModel, SIGNAL(newDataAreThere()), AnotherView, SLOT(notifyNewDataReceived()));

You can connect/disconnect your views, and the model doesn't even know who are listening to him... That's not his problem...

I hope my little example is understandable ;)

Andy M
thanks for your contribution **but** this did carefully read the question?
jldupont
Oups, yeah, I read a bit too quickly your question... Sorry !
Andy M