PDA

View Full Version : QThread and New Object Creation



pratham_shah
25th April 2013, 08:41
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 ?

wysota
25th April 2013, 09:01
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.

anwar.qt
25th April 2013, 16:09
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?

wysota
25th April 2013, 16:58
Read the file and prepare object creation in a worier thread and then create objects in the main thread.

Santosh Reddy
25th April 2013, 17:40
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).

Santosh Reddy
26th April 2013, 07:18
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.

anda_skoa
26th April 2013, 12:11
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,
_