QThread for QQuickPaintedItem
-
hello,
usually when i have c++ object doing havy tasks and i need that object in QML ,
i create another 'interface class' for it, in the interface classe i create a qthread and the timeconsuming object , i move the time consuming object to that thread, then i use time consuming methods through signal/slot machanisme by exposing my interface class to qmlMyInterfaceClass obj; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextProperty("myModel", &obj);
but this time my worker object is a QQuickPaintedItem and i need to declare one in QML , so i need to
qmlRegisterType<MyQQuickPaintedItemWithTimeConsumingMethods>("GCodeView", 1 ,0 , "Gcode");
how to move this to a thread please ?
-
@LeLev : Hi, actually I do not have a answer for your question but if you do not mind I would like to see your code, i.e. how u create the interface class and run on a thread? I have a similar use case.
Thank you -
@VinayBalajiRajputh like here
-
I'd strongly warn against moving an item which is owned by the QML engine into another thread, as this would thread affinity issues with the internal QQmlData for the object, which is managed by the engine.
Instead, I'd recommend that your MyQQuickPaintedItemWithTimeConsumingMethods has a worker object as a member or that it spawns a worker function in another thread (for intance via QtConcurrent::run).
The expensive method in your class would then just call the corresponding method of the worker, and it would forward the worker's done signal to your classes users.
If the data you work on is cheap to copy, I would just copy it for ease of programming; if the data is expensive to copy, you'll have to pass references to the worker and protect your data with locks.