Question

When I use Q_PROPERTY in other QThread I have error: Illegal attempt to connect to ... that is in a different thread than the QML engine.

//myclass.cpp
Q_PROPERTY(QString FileReady READ GetFileReady NOTIFY FileReadyChanged)

This is in other QThread. In main thread I have QtQuick2ApplicationViewer and o Connect Q_PROPERTY like that:

//main.cpp
    MyClass cObject1();

QThread cThread1;                     //create new thread
cObject1.DoSetup(cThread1);           //connect
cObject1.moveToThread(&cThread1);     //move work to other thread
viewer.rootContext()->setContextProperty("otherthread",&cObject); // propably bad line

How could I send property from myclass in other thread to QML in main thread ? If you could, give a small example how to do it.

Was it helpful?

Solution

Create class "Model" which object will stay in main thread and connect it with QML. Create "Worker" class which do what you want to do in other thread and send updates from object of this class to object of "Model" class (default connection should do the trick). In other directions "Model" should schedule tasks for "Worker".

This way you will keep threads away form QML engine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top