Set property values by importing data to ValueSource.qml from different Thread/Runtime
-
Hello,
is there a way to make the Qt Extras Example Dashboard import it's values from a different thread?The dashboard has the qml file dashboard.qml which runs looping sequential and parallel animations to set values of the properties. Only difference to the example is that i converted it to run with cmake with a CMakeLists.txt file instead of a *pro qmake project.
Instead of setting the values of the properties in this looping animations, i want them to be imported from a .hpp file that is running in a different thread.
In my main.cpp i run two threads, one to start the dashboard and a separate one, that receives data from outside the system by running a process in a own runtime. In this separate thread runs a looping function in a .hpp file, that sets the values of some integer variables.
I already know about the usage of lockguards in C++ to make two different threads write and read the same variable, but i don't know how to accomplish this in the qml file and if this is even possible.Any advice on how to get the data from the .hpp file to be displayed on my dashboard?
I'm also happy to hear any suggestions, that use a completely different approach.Thank you in advance & kind reagards
-
Hi there,
i found an utterly easy solution to exercise everyhting with using a QObject, wihtout having to use QThread.
In mymain.cpp
started i start my GUI after the separat runtime using std::thread.
I created a QObject in a .hpp and .cpp class with a slot and a signal. I had to addQQmlContext* context = engine.rootContext(); context->setContextProperty("signalData", separat_process.pointer); engine.load(QUrl("qrc:/qml/dashboard.qml"));
to my main to connect my GUI to the Object. In the separat_process i use the slots of the QObject which triggers signals.
Then i had to addConnections { target: signalData function onXYZ(abc) { //something happens } function onXYZ(abc) { //something else happens } }
to my GUI Item.
-
Hi. @Philomath-Qt
I faced the same problem. Have a look at my question at https://forum.qt.io/topic/122174/how-to-eliminate-msleep-from-a-worker-thread-and-use-a-timer-in-the-gui-thread-instead
It describes how I first tried to implement a C++ thread in a horrible way to update my QML, before a better and much more cleaner solution was suggested by @SGaist. As it turned out, I just needed to read the Qt docs better, as the solution was there all along. -
Hi @Diracsbracket and thank you for your answer.
I was working on another but similar approach Exposing Attributes of C++ Types to QML , i was inspired by a stackoverflow post, but always get to a very common error messageundefined reference to "vtable for Worker"
as soon as i addQQuickView view; Worker msg; view.engine()->rootContext()->setContextProperty("msg", &msg); view.setSource(QUrl::fromLocalFile("MyItem.qml")); view.show();
to my thread.
I'm trying for hours to fix it, but none of the suggestions online helped.
I wanted to create a QObject in the one thread, my *.hpp file and then read the properties from my *.qml fileI will try to use your approach and hope i won't encounter the same problem.
-
Hi there,
i found an utterly easy solution to exercise everyhting with using a QObject, wihtout having to use QThread.
In mymain.cpp
started i start my GUI after the separat runtime using std::thread.
I created a QObject in a .hpp and .cpp class with a slot and a signal. I had to addQQmlContext* context = engine.rootContext(); context->setContextProperty("signalData", separat_process.pointer); engine.load(QUrl("qrc:/qml/dashboard.qml"));
to my main to connect my GUI to the Object. In the separat_process i use the slots of the QObject which triggers signals.
Then i had to addConnections { target: signalData function onXYZ(abc) { //something happens } function onXYZ(abc) { //something else happens } }
to my GUI Item.