Results 1 to 5 of 5

Thread: Create an object instance in separate thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Create an object instance in separate thread

    Hi Community,
    At the time of running my application, I need to instantiate an object.
    The object creation takes time (about 10 seconds) and this leads to the consequence that the GUI is not shown until the object is created.

    To work around this I thought of creating the instance in a second thread (thread B) but in this case the thread B is the owner of the instance is that right?
    How can I transfer the ownership of the object to the main thread once it's been created? Is there any better solution?

    Regards,
    Franco
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    489
    Thanks
    11
    Thanked 71 Times in 69 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Create an object instance in separate thread

    Hi, you can transfer the object to the main thread using
    Qt Code:
    1. myObject->moveToThread(QApplication::instance()->thread());
    To copy to clipboard, switch view to plain text mode 
    .

    Another option would be to show a splashscreen while your object is initializing. Maybe even show some progress using QSplashScreen::showMessage().

    Ginsengelf

  3. The following user says thank you to Ginsengelf for this useful post:

    franco.amato (9th May 2022)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,153
    Thanks
    295
    Thanked 854 Times in 841 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create an object instance in separate thread

    To work around this I thought of creating the instance in a second thread
    Keep in mind that GUI instances (inherited from QWidget) can only be created in the main thread, AFAIK. I am not sure if you can create one in another thread and then transfer it to the main thread unless it can be created without a parent and then transferred and given a parent from the main thread.

    Every QWidget instance has a QWidget parent, and they can only live in the main thread.

    You could also do things in reverse - create the object in the main thread, initialize it in the second thread, then move it back.

    Personally, I use a splash screen where there are slow startup issues and keep the user entertained that way.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (5th May 2022)

  6. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    681
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create an object instance in separate thread

    Quote Originally Posted by d_stranz View Post
    Keep in mind that GUI instances (inherited from QWidget) can only be created in the main thread, AFAIK. I am not sure if you can create one in another thread and then transfer it to the main thread unless it can be created without a parent and then transferred and given a parent from the main thread.

    Every QWidget instance has a QWidget parent, and they can only live in the main thread.

    You could also do things in reverse - create the object in the main thread, initialize it in the second thread, then move it back.

    Personally, I use a splash screen where there are slow startup issues and keep the user entertained that way.
    The object I need to create is not derived from QWidget
    Franco Amato

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,153
    Thanks
    295
    Thanked 854 Times in 841 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Create an object instance in separate thread

    The object I need to create is not derived from QWidget
    Then you should be OK if you follow @Ginsengelf's suggestion. Still, you should be careful to set up a mechanism where your initialization thread signals the main thread when it is finished so you do not start using the object you are initializing before that process is complete.

    A splash screen still seems like a simpler approach
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    franco.amato (9th May 2022)

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2015, 09:06
  2. Best way to acces MainWindow instance from separate class
    By scarecr0w132 in forum Qt Programming
    Replies: 6
    Last Post: 14th January 2014, 06:04
  3. How to Have a class run in a separate thread.
    By sona1111 in forum Newbie
    Replies: 7
    Last Post: 29th August 2013, 07:44
  4. GUI Updates from separate Thread
    By sa5webber in forum Newbie
    Replies: 5
    Last Post: 16th June 2012, 21:08
  5. new QWidget in separate thread
    By magland in forum Qt Programming
    Replies: 15
    Last Post: 7th February 2008, 13:32

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.