Results 1 to 8 of 8

Thread: Slots & Signals w/ parameters passed by reference.

  1. #1
    Join Date
    Jun 2006
    Location
    Colorado
    Posts
    12
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Slots & Signals w/ parameters passed by reference.

    I'm working on a (1) object that is needs to signal another (2) object that some data is ready for processing. But I need to distinguish if the (2) object needs to initialize how the data is processed for the first time. I had thought that I could pass a boolean value via pass-by-reference and then allow the slot to change it so when some more data is ready to be processed the slot wouldn't initialize it the second time around.

    Example Code:

    Qt Code:
    1. connect(this, SIGNAL(readySendData(bool &, MyData &)),
    2. this, SLOT(sendData(bool &, MyData &)));
    3.  
    4.  
    5. void DataProcessor::sendData(bool & intializationNeeded, MyData & data)
    6. {
    7. if(intializationNeeded)
    8. {
    9. // Do Initialization Stuff
    10. intializationNeeded = false;
    11. }
    12. else
    13. {
    14. // Do Stuff
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 


    Hopefully this is clear but I would like to check to see if initialization is needed. Then the slot will change the parameter from true to false. So then next time the signal readySendData is generated it will enter the else code block. Is this possible with Slots and Signals or does it violate the loose coupling scheme.
    Last edited by jpn; 18th December 2008 at 22:12. Reason: missing [code] tags

  2. #2
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slots & Signals w/ parameters passed by reference.

    It should work.

  3. #3
    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: Slots & Signals w/ parameters passed by reference.

    Wouldn't it be better to send a signal to the original emitter that you have initialized something and then next time it (the original emitter) would emit a signal with a boolean value set to false meaning that the data needn't be evaluated?

    Sending references via signals is a really bad idea

  4. The following user says thank you to wysota for this useful post:

    Wazman (22nd December 2008)

  5. #4
    Join Date
    Jun 2006
    Location
    Colorado
    Posts
    12
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slots & Signals w/ parameters passed by reference.

    Quote Originally Posted by wysota View Post
    Wouldn't it be better to send a signal to the original emitter that you have initialized something and then next time it (the original emitter) would emit a signal with a boolean value set to false meaning that the data needn't be evaluated?

    Sending references via signals is a really bad idea
    Wysota

    Can I ask why is sending references via signal is a really bad Idea?

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Slots & Signals w/ parameters passed by reference.

    Signals can be connected with any slot of any class.
    Say if some unintended class catches ur signal and modifies the data in a wrong manner ???
    Isnt it a bad idea

  7. #6
    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: Slots & Signals w/ parameters passed by reference.

    Furthermore it's not possible to send such a signal across threads.

  8. #7
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slots & Signals w/ parameters passed by reference.

    Quote Originally Posted by wysota View Post
    Furthermore it's not possible to send such a signal across threads.
    That's true (in fact, it holds for queued connections in general),
    and there also the mentioned pitfalls with multiple connection.

    But still, it works for direct connections and makes the code
    on the sender side much simpler. A second connection for
    passing the result back forces the sender to split the calling
    function, and there isn't a guarantee that other object connect
    to the return slot either.

  9. The following user says thank you to ktk for this useful post:

    Wazman (22nd December 2008)

  10. #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: Slots & Signals w/ parameters passed by reference.

    The design is nasty. I'm sure you could think of something better. Maybe using signals and slots is not the right way to go here?

  11. The following user says thank you to wysota for this useful post:

    Wazman (22nd December 2008)

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  3. MS Sql native driver??
    By LordQt in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2007, 13:41
  4. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  5. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15

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.