Results 1 to 5 of 5

Thread: Passing data to a QThread

  1. #1
    Join Date
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Passing data to a QThread

    Hello everyone,

    I'm trying my first implementation of a QThread, and I'm trying to pass some data to the thread. It is to be used as a worker thread to keep the GUI responsive, so multiple threads are not going to be created.

    Here is the class definition. I'd like to pass in data to the thread using references to prevent a copy from occurring. The thread will be modifying the original data as well.

    Qt Code:
    1. class ProcessThread : public QThread
    2. {
    3. public:
    4. void run();
    5. void setSettings(qpiv_settings &settings_in);
    6.  
    7. private:
    8. qpiv_settings settings;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ProcessThread::setSettings(qpiv_settings &settings_in)
    2. {
    3. settings = settings_in;
    4. }
    To copy to clipboard, switch view to plain text mode 

    And here is the basic call to start the thread:
    Qt Code:
    1. ProcessThread *thread = new ProcessThread;
    2. thread->setSettings(settings);
    3. thread->start();
    To copy to clipboard, switch view to plain text mode 

    I've done some small tests, and it doesn't appear like if I modify the values within the thread that it carries back to the original calling function. Am I missing something fundamental?

    Thanks in advance!!

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Passing data to a QThread

    I don't think you are using the reference correctly.

  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: Passing data to a QThread

    You are making a copy in your setSettings call. Passing a reference is not enough if you immediately copy the object. And what you're doing is not thread-safe, prepare to experience random crashes.
    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
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Passing data to a QThread

    Thanks for the quick replies. I've always made copies before when I deal with classes because the data is small. However, the settings variable will end up being very large, so is there a way to just have the reference in the class? I'm sorry if this is a very basic C++ question.

    wysota, do you have any short suggestion on a thread-safe implementation? My main thread reads in very large images and has associated settings, so the data resides in the main thread. I'd like to use this second thread to operate on the image data and settings without having to make a second copy. Then, when the secondary thread finishes, I'd like to be able to use the main thread to continue and display the data, etc.

    Thanks again!

  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: Passing data to a QThread

    Quote Originally Posted by lynchkp View Post
    Thanks for the quick replies. I've always made copies before when I deal with classes because the data is small. However, the settings variable will end up being very large, so is there a way to just have the reference in the class? I'm sorry if this is a very basic C++ question.
    Sure there is. But you don't have a reference but a copy, just look at your class definition.

    wysota, do you have any short suggestion on a thread-safe implementation?
    A short suggestion is to synchronize access to a shared resources with a mutex or a semaphore.

    My main thread reads in very large images and has associated settings, so the data resides in the main thread. I'd like to use this second thread to operate on the image data and settings without having to make a second copy. Then, when the secondary thread finishes, I'd like to be able to use the main thread to continue and display the data, etc.
    If your main thread reads large images then your second thread will not help you prevent freezing the user interface.
    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.


Similar Threads

  1. Passing data via signal/slot
    By gib in forum Qt Programming
    Replies: 4
    Last Post: 1st November 2010, 06:49
  2. passing data when closing a dialog
    By jimiq in forum Newbie
    Replies: 4
    Last Post: 4th November 2009, 17:48
  3. QThread Data Access Method
    By schan117 in forum Qt Programming
    Replies: 7
    Last Post: 18th August 2009, 07:25
  4. Passing a child from a QThread to a QMainWindow
    By sgtbash in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2009, 09:30
  5. Replies: 2
    Last Post: 27th March 2007, 13:09

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.