PDA

View Full Version : Periodically access QML variable from C++



mekarim
23rd November 2015, 21:34
Dear Qt Specialists!

I would like to access, lets say every 10 millisecond, a certain QT-QML variable (I am not even sure if I am speaking the proper wording) from C++ and use that information to run some code in C++ to manipulate some external, not connect through the QML, platform.

For example: imagine, QML provides a particular value from a certain sensor, which is continuously updating. I would like to periodically access this from C++ to move the mouse cursor in linux.

Any suggestions? Thank you all.
mekarim

anda_skoa
24th November 2015, 07:45
- Create a QObject based class with a property of the type you want.
- Create an instance of that class and set it as a context property on the QQmlEngine's root context
- Write to that property in QML
- React to the value change in the property's setter function or run a QTimer that reads the stored value

Cheers,
_

mekarim
24th November 2015, 08:13
Dear Anda_skoa

Thank you so much. I think I partially understand what you are saying.

Do you mind sharing one example, please? A short one would suffice as well.

Thank you again.
Cheers

anda_skoa
24th November 2015, 11:08
A bit like this http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html#setting-an-object-as-a-context-property

They don't use a property but the principle is similar.

Cheers,
_

mekarim
24th November 2015, 11:45
Thank you so much. I think this will do. I will check and update.

Cheers