Results 1 to 7 of 7

Thread: QThread and New Object Creation

  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    22
    Thanks
    3
    Qt products
    Qt4

    Default QThread and New Object Creation

    Hi,

    I am developing an application where in I am providing facilities like Import, Merge.
    As Import, Merge are time consuming operation I perform these operations on a thread. I use QtConcurrent::run() method or QObject::moveToThread() method along with appropriate connects to perform the operation.
    Firstly can you please specify which method is best suited and I should use ?
    My problem is, I am creating new custom QGraphicsItem inside these function and adding some connects inside the constructor.
    As these objects are created as a part of the thread that it is running I have to use moveToThread(QCoreApplication::instance()->thread());
    Am I doing the right thing or is there any other way to solve this ?
    Also after thread completion I move to the main application thread and add these custom QGraphicsItem to the scene.
    I am facing problem when I interact with these QGraphicsItems giving problems such as "QObject: Cannot create children for a parent that is in a different thread." or sometimes "ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread."
    What am I doing wrong ? Can you please solve the issue ?

  2. #2
    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: QThread and New Object Creation

    There is no single "best method", everything depends on what you are doing.

    Manipulating UI objects from within worker threads is Bad and you shouldn't do that. Create and manipulate your graphics items in the main thread instead.
    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.


  3. #3
    Join Date
    Sep 2012
    Location
    Pune, India
    Posts
    18
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: QThread and New Object Creation

    I have similar issue as mentioned in above post.

    We are developing an application like paint, we draw rectangles (consider 10000 of them)of different size and color. We write them in a file in a file, when we open file again we read layout information (i.e. size and color) and create rectangle (i.e. QGraphicsItem).

    The opening file with large number of rectangle (It may contain upto 45000) consumes massive time. Application can open multiple files in different tab (Each tab associated with a file).

    So we are trying to run opening operation on another thread. Such that application (UI) won’t freezes during open, and in mean time user will be able to construct a new file or work on already open files.

    We are facing similar issues if we create rectangles in thread other than main (UI) thread, and if we create rectangles in main thread it will freeze the application.

    So I wanted to know that, how multithreading mechanism should be implemented in above scenario?

  4. #4
    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: QThread and New Object Creation

    Read the file and prepare object creation in a worier thread and then create objects in the main thread.
    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.


  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QThread and New Object Creation

    As far a QGraphicsItem is concerned it is not derived from QObject, so one can create QGrphicsItem objects (QGraphicsTextItems, QGraphicsRectItems etc) in a worker thread. One thing has to taken care here that these items have to be created in worker thread and then moved to UI thread (no need to use moveToThread()) and only then they should be added to the QGraphicsScene (in the UI thread).
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    anwar.qt (26th April 2013)

  7. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QThread and New Object Creation

    As such QObjects can also be created in worker threads ans moved to UI thread, but need to take care of parent ownership properly while creation and moving.
    Last edited by Santosh Reddy; 26th April 2013 at 06:40.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QThread and New Object Creation

    Even if you create the items in the worker thread, the expensive operation is more likely adding the item to the scene, because the scene has to recalculate all kinds of things (visibility, etc).

    So it might work even without additional thread if you process only a couple of items (say 10) at a time, returning to the event loop afterwards, e.g. using a timer to trigger processing the next batch.

    Cheers,
    _

Similar Threads

  1. Cancelling an Object Creation
    By bbad68 in forum Newbie
    Replies: 1
    Last Post: 4th March 2010, 20:22
  2. Replies: 7
    Last Post: 15th January 2010, 20:45
  3. Multiple start() on same QThread Object
    By mdecandia in forum Qt Programming
    Replies: 4
    Last Post: 3rd November 2009, 08:14
  4. Replies: 1
    Last Post: 2nd March 2009, 19:32
  5. Multiple form object creation
    By kpmsivachand in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2009, 01:09

Tags for this Thread

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.