Results 1 to 4 of 4

Thread: broadcasting signals through the application

  1. #1
    Join Date
    Jul 2012
    Posts
    247
    Thanks
    29
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile broadcasting signals through the application

    Let's say I have a signal that I want to make available to several components of my program throughout the entire project. Let's also say there might be more than one component that might trigger or emit that signal.

    Does it make sense to keep a global instance or a singleton of a class that manages such "global" signals?


    I mean like this:


    Qt Code:
    1. class SignalCollection: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. void EmitLogMsg(QString msg) {emit sigLogMsg(msg);}
    6. void EmitSettChange() {emit sigSettChange();}
    7.  
    8. signals:
    9. void sigLogMsg(QString msg);
    10. void sigSettChange();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Now receivers from anywhere can connect() to the signals with the SignalCollection as the sender. And senders from anywhere can just call EmitSettChange() from anywhere to emit such a signal.


    The advantage is that this breaks up the relation between sender and receiver.


    Is that a common pattern? Are there better ways to do this? Or are there any good reasons why I should NOT do that?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: broadcasting signals through the application

    You'll first have to make your EmitLogMsg() etc. methods public so other classes can call them, and it would make more sense to make these slots as well so callers can connect their own signals to them. You also need to make a private constructor so you can enforce the singleton rule, and maybe a static getInstance() method that will construct the singleton if needed or return it if already constructed.

    The major downside to this is that end receiver of the singleton's signal has no clue where it came from. You've broken the sender() connection by relaying it.

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

    Default Re: broadcasting signals through the application

    IMO with the right design the receiver shouldn't care who is the sender of the signal. Such approach promotes loose coupling (which after all is all signals are about).
    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.


  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: broadcasting signals through the application

    Yes, that's absolutely true. But it sure helps during debugging when you can arrive at a point from 10 different directions and you'd like to know which one it actually was. Looking at stack traces gets boring after a while.

Similar Threads

  1. Replies: 5
    Last Post: 26th July 2012, 18:43
  2. Timer signals in non GUI application
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2010, 10:52
  3. Replies: 8
    Last Post: 29th June 2010, 07:10
  4. Basically signals-slot application
    By Daxos in forum Newbie
    Replies: 4
    Last Post: 27th May 2010, 13:56
  5. Broadcasting a signal ?
    By squidge in forum Qt Programming
    Replies: 8
    Last Post: 16th November 2009, 23:07

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
  •  
Qt is a trademark of The Qt Company.