Results 1 to 12 of 12

Thread: One Signal - many Slots

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default One Signal - many Slots

    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).

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: One Signal - many Slots

    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.

  3. #3
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: One Signal - many Slots

    Quote Originally Posted by Lykurg View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: One Signal - many Slots

    Quote Originally Posted by ggdev001 View Post
    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?

  5. #5
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: One Signal - many Slots

    Quote Originally Posted by Lykurg View Post
    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).

  6. #6
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: One Signal - many Slots

    which I may not want
    So you can add this to your class B, say:
    Qt Code:
    1. class B {
    2. enum State {CHANGE_TEXT, UPDATE_ITEMS};
    3. ...
    4. State current_state;
    5. ...
    6. public slots:
    7. void MySlot () {
    8. switch (this->current_state) {
    9. case State::CHANGE_TEXT:
    10. ... etc ....
    11. };
    12. };
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: One Signal - many Slots

    Quote Originally Posted by lanz View Post
    So you can add this to your class B, say:
    Qt Code:
    1. class B {
    2. enum State {CHANGE_TEXT, UPDATE_ITEMS};
    3. ...
    4. State current_state;
    5. ...
    6. public slots:
    7. void MySlot () {
    8. switch (this->current_state) {
    9. case State::CHANGE_TEXT:
    10. ... etc ....
    11. };
    12. };
    13. }
    To copy to clipboard, switch view to plain text mode 
    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??).

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: One Signal - many Slots

    How do you determine what you wish to update?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: One Signal - many Slots

    Quote Originally Posted by wysota View Post
    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 ...

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: One Signal - many Slots

    Quote Originally Posted by ggdev001 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: One Signal - many Slots

    And if you want to rule them all , you can subclass QEvent, and take advantage of Qt event system.

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: One Signal - many Slots

    Quote Originally Posted by ggdev001 View Post
    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,
    _

Similar Threads

  1. One signal more slots
    By paolom in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2012, 11:48
  2. Signal/Slots, Where can i use it?
    By Tio in forum Newbie
    Replies: 2
    Last Post: 25th May 2010, 01:36
  3. Signal and Slots
    By waynew in forum Newbie
    Replies: 3
    Last Post: 20th November 2009, 03:50
  4. signal and slots
    By vermarajeev in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2007, 08:31
  5. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.