QML and Qt integration
-
Okay I believe I have read just about everything on this topic that is available 15 times and Im on the verge of just using Qt because I dont think I can do what I want.
I would like my software to work in the following way:
- Qt backend which runs threads that read and write from a serial port and run a GUI. Done.
- QML front end which displays the value recieved on the serial port. and can send information on a button click down the serial port.
- The backend should talk to the frontend via signals and slots for modularity.
Sounds simple enough.
There are a number of different peices of information sent over the serial port for example: speed is 100, rpm is 2000, temperature is 90.5. I have a gauge QML file that I use 3 times to display all this information, I set it up like this in qml:
Gauge { name: "Speed"; minimum: 0.0; maximum: 150.0; value: 0; msg_par: "1"}
Gauge { name: "RPM"; minimum: 0.0; maximum: 8000.0; value: 0; msg_par: "2"}The gauge widget will adjust scale according to max and min values. A serial message has two pieces of information, msg_par and a value.
Now what I would like to do is when a message is received via serial of "msg_par" 1 it will update the Speed gauge. When a message is received via serial of "msg_par" 2 it will update the RPM gauge. I would like to signal the Speed update into QML but im not sure how to find the child via "name" or "msg_par" to update the value parameter. Im also not sure this is a good idea becuase then Qt will be reaching into the QML implementation a breaking the modularity rule. The other problem is the QUI will not always be displaying the gauges so the gauge child will not always exist.Sending messages from the GUI down the serial port was reasonably easy I initialised and COMMS class and exposed it to QML, so QML can call a send function in the COMMS class which passes information out.
Any ideas on how to update QML parameters. Or maybe someone could suggest a simpler method to implement this. Im more of a c programmer than cpp so some of the cpp concepts are new to me.
Cheers. Mitch.
-
Duh, that's why I don't like answering "C++/QML communication" questions too much. There are just so many options available... but all are rather hard to explain.
If you don't like findChildren<>)() approach, then maybe create a QObject subclass containing all the properties you need (rpm, speed, etc.), and then add it as a context property to the QML context. Then you can simply assign those properties in QML.
@
QObjectSubclass *myObj = new ...;
QQuickView *view = ...;
view->rootContext()->setContextProperty("myObjectName", myObj);// In QML
Gauge {
name: “Speed”; minimum: 0.0; maximum: 150.0;
value: myObjectName.property;
msg_par: “1”
}
@Or you can work using that object and signals and slots (it's possible to connect signals and slots between C++ and QML).