PDA

View Full Version : One Signal - many Slots



ggdev001
21st February 2013, 08:44
Hi,

how is it possible to handle the SAME signal of the SAME C++ object in different ways using different slots??
I know I can attach different slots to this signal -- but when the signal will get called it will invoke all slots,
which is what I don't want.
I want the signal to invoke one slot at a particular point in time (the one I specify).

Lykurg
21st February 2013, 08:53
The easiest would be to have only one slot. In it you decide which function is to be called at the specific time and call it yourself. Or if any condition change you can disconnect the last slot and connect the signal to the now right slot.

ggdev001
21st February 2013, 08:58
The easiest would be to have only one slot. In it you decide which function is to be called at the specific time and call it yourself. Or if any condition change you can disconnect the last slot and connect the signal to the now right slot.

when the signal emits and I enter that one slot, how do I differentiate which action to take?? as I said there is one signal.

Lykurg
21st February 2013, 09:05
how do I differentiate which action to take?Well you are the author of the application. You should know.

But maybe I get you wrong. Can you give an example of what you are trying and about what signal of what class you are talking. Do you emit it yourself?

ggdev001
21st February 2013, 09:21
Well you are the author of the application. You should know.

But maybe I get you wrong. Can you give an example of what you are trying and about what signal of what class you are talking. Do you emit it yourself?

Hi, thanks. Yes, I emit it myself it is a custom signal. Some Class A emits this signal in my app. Say class B is interested in this signal. The restriction we have is we want to use only one instance of class B that will listen to this signal. What we want is, sometimes using the response of the signal say we want to update only the text of the button, but in another dialog, we may want to update list items using the data that is in the signal response. This signal is called after I call some function of Class A. Problem is each time the signal is emitted it will call both above methods (change button text and update list items, which I may not want, rather at one point in time, in reaction to the signal I only want the button text to be updated, and at another point in time, I want only the list items to be updated).

lanz
21st February 2013, 09:51
which I may not want

So you can add this to your class B, say:

class B {
enum State {CHANGE_TEXT, UPDATE_ITEMS};
...
State current_state;
...
public slots:
void MySlot () {
switch (this->current_state) {
case State::CHANGE_TEXT:
... etc ....
};
};
}

ggdev001
21st February 2013, 10:26
So you can add this to your class B, say:

class B {
enum State {CHANGE_TEXT, UPDATE_ITEMS};
...
State current_state;
...
public slots:
void MySlot () {
switch (this->current_state) {
case State::CHANGE_TEXT:
... etc ....
};
};
}

that is also an option...but imagine,

I make two connects

connect(classAObj1, customSignal, classBObject, Slot);
connect(classAObj2, customSignal, classBObject, Slot);

then I set:

current_state = CHANGE_TEXT; // and now I am waiting for a server response which will emit
// signal and I must respond with CHANGE_TEXT behaviour

in the mean time, if I fire the signal also another time -- but not I want UPDATE_ITEMS behaviour
and I set
current_state = UPDATE_ITEMS; // and now comes the signal which was fired for CHANGE_TEXT,
I will still receive behavior for UPDATE_ITEMS...

I know this is tricky.. I will have to think myself also .. what to do in such cases... (solution may involve in any case passing some identifier to the
signal maybe which will return it back -- and I use it in the switch statement??).

wysota
21st February 2013, 10:39
How do you determine what you wish to update?

ggdev001
21st February 2013, 10:46
How do you determine what you wish to update?
Yes that's a good question but I don't know if I can say it now exactly. I am kind of raising these concerns in advance.
Because I have an app and UI part of my app is using these signals, and clearly, at some point in time, I may
need to to just update some list items maybe, at another time (as I mentioned I need to use one signal and one class instance),
I may need to update some other UI element and etc. I don't know it depends how my app will be structured in terms of UI... later on ...

wysota
21st February 2013, 11:15
Yes that's a good question but I don't know if I can say it now exactly.
Without answering this question it is hard to start implementing anything.


Because I have an app and UI part of my app is using these signals, and clearly, at some point in time, I may
need to to just update some list items maybe, at another time (as I mentioned I need to use one signal and one class instance),
I may need to update some other UI element and etc. I don't know it depends how my app will be structured in terms of UI... later on ...

In general if you want to do different things, you usually do that using different functions, not one function to rule them all.

lanz
21st February 2013, 12:50
And if you want to rule them all :D, you can subclass QEvent, and take advantage of Qt event system.

anda_skoa
22nd February 2013, 13:19
connect(classAObj1, customSignal, classBObject, Slot);
connect(classAObj2, customSignal, classBObject, Slot);


But in this case you have two different senders, so Slot can decide what to do depending in the pointer returned by QObject::sender().
Or if customSignal does not have any arguments, going through QSignalMapper.

Cheers,
_