Results 1 to 5 of 5

Thread: Passing data via signal/slot

  1. #1
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    112
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Passing data via signal/slot

    I want to pass a significant amount of data between threads, using the signal/slot mechanism. I was planning to pass a pointer to the data, but I have learned that this is frowned on. E.g. wysota wrote: "Never emit pointers (or non-const references) across threads, they might become invalid before the receiving thread has a chance to process them. Make a copy and send the copy instead."

    I believe I can make sure that the pointer doesn't become invalid, but I don't like to go against the advice of someone much more expert than me. What would be the recommended approach when creating data copies is time-consuming? The app is transmitting data to update a VTK scene that has moving actors.

  2. #2
    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: Passing data via signal/slot

    If your data is huge, sending a copy might be expensive. In that case you may pass pointer. Though should be avoided and make sure the data is still valid in the slot.

    Other thing you can do is emit a signal saying that data is ready. And provide some IPC mechanism for data transfer.

  3. #3
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    112
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing data via signal/slot

    Thanks. I don't know anything about IPC mechanisms. Could you provide a pointer?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Passing data via signal/slot

    Or, keep the data in a file or other common place. Then use mutexes when accessing or writing data (or use qtconcurrent and don't have to deal with that complexity)

  5. #5
    Join Date
    Mar 2010
    Location
    Auckland, NZ
    Posts
    112
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing data via signal/slot

    QMutex and a data buffer in global scope looks like a good way to go. Simpler than shared memory.

Similar Threads

  1. Problem with passing Pointers to a Slot
    By Basti300 in forum Newbie
    Replies: 2
    Last Post: 26th May 2010, 14:45
  2. Passing an integer to a slot
    By bizmopeen in forum Newbie
    Replies: 6
    Last Post: 30th October 2009, 09:51
  3. disconnect SIGNAL/SLOT directly after emitting data
    By donglebob in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2009, 22:53
  4. passing arguments in SLOT
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 3rd July 2008, 21:55
  5. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04

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.