Results 1 to 6 of 6

Thread: Signals and Slots question

  1. #1
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Signals and Slots question

    Docs say "Signals and slots are loosely coupled: A class which emits a signal neither knows nor cares which slots receive the signal". What if that was not what was desired? What if you had an emitting class that was to send messages to some arbitrary number of widgets. Say it was a socket class receiving messages over a lan and the messages were addressed each to go to some particular widget and you wanted the message passed using signals/slots. As each widget is created the signal/slot connection is setup and the widget then awaits possible incoming messages. What I don't see is how "emit" could be used to emit to a particular widget. From what I'm reading in the docs, it seems like there really isn't a way to place the burdon on the "emitter" to send things only to one target. Instead, when it emits it emits to all targets(widgets) and it's the widgets responsibility to decide if the message is meant for it? I hope I'm making sense....I can see how to connect a signal to many slots. Like connecting a number of wires from one source to many widgets. This serves the purpose of making the connections. But, I don't want to send a message to a widget that it wasn't intended for. My conundrum is that I don't see how to use signals/slots "connect" and/or "emit" syntax to do this????

  2. #2
    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: Signals and Slots question

    Quote Originally Posted by Thoosle View Post
    What I don't see is how "emit" could be used to emit to a particular widget.
    It can't be used this way directly. Signals are public.

    From what I'm reading in the docs, it seems like there really isn't a way to place the burdon on the "emitter" to send things only to one target. Instead, when it emits it emits to all targets(widgets) and it's the widgets responsibility to decide if the message is meant for it?
    Correct. You can help yourself by using a QSignalMapper
    But, I don't want to send a message to a widget that it wasn't intended for.
    Don't use signals and slots then. Just register objects in the "emitter" and call its methods directly from the emitter.

    My conundrum is that I don't see how to use signals/slots "connect" and/or "emit" syntax to do this????
    You don't have to force using signals and slots everywhere

    Qt Code:
    1. Emitter emitter;
    2. Receiver rcv1, rcv2, rcv3;
    3. emitter.register(&rcv1);
    4. emitter.register(&rcv2);
    5. emitter.register(&rcv3);
    6. //...
    7.  
    8. void Emitter::emitMessage(int id, const QString &msg){
    9. Receiver *rcv = _registered[id]; // QMap<int, Receiver*>
    10. rcv->receive(msg);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signals and Slots question

    wysota, thanks for the insight and guidance, very helpful.

  4. #4
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signals and Slots question

    wysota, When I look at the docs for QSignalMapper I believe here llies the solution to the problem I was describing. Apparently, when a widget is created I can set a mapping to it from the "emitter" based on the widgets id so that signals from the "emitter" are mapped to the appropriate widget. I will "connect" to all the widgets but only send messages to their intended destination, which is the desired operation. In the QSignalMapper "Detailed Description" it examples using QSignalMapper to setmappings for a group of buttons to signal a slot with eachs buttons text id thus bundling the buttons signals. What I'm wanting to do is like the inverse of that. Do you agree my conclusions in this or have I missed the point? BTW, I'm thinking that the "emitter" and widgets will run in their own threads, hence the desire for signals/slots. Thanks again for your insights....

  5. #5
    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: Signals and Slots question

    QSignalMapper does a many-to-one mapping and as you said, you want the opposite. The code I've written in the previous post more or less does exactly that. It just isn't thread safe, so if you wish to use threads for your receivers, you need to modify it. I would even suggest to use custom events instead of signals and slots, this way instead of rcv->receive(...) you'd simply call QCoreApplication::postEvent(rcv, new MessageEvent(...)). Then it's just a matter of reimpementing customEvent() to handle the event and implementing the custom event class (MessageEvent).

  6. #6
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signals and Slots question

    wysota, this discussion is most helpful and enlightening for me. I'll take a look at your suggestions. Again, thanks very much for your insights....

Similar Threads

  1. Signals and Slots (with structure as parameters)
    By vishwanath in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:14
  2. Nested signals and slots
    By vishva in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2006, 09:47
  3. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 08:12
  4. Order of signals and slots
    By Morea in forum Newbie
    Replies: 1
    Last Post: 26th February 2006, 22:09
  5. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35

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.