[Solved] Sending C++ values to QML properties
-
Hi,
I trying to change some QML properties from C++ in order to rotate one object but looks like it doesn't update or repaint. I followed this document:
http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html
...but once i change the property i debug it, the value is correct but the object didn't rotate. This is how i change the property and how i read it with the debug:
object->setProperty("canvas3d.xRotSlider", 90);
qDebug() << "Property value:" << object->property("canvas3d.xRotSlider").toDouble();Any suggestion?
Thanks! -
My starting project was the barrel example, it includes js with qml, but instead to rotate the barrel with the sliders i would like to rotate with a given C++ variables values. But i had no success sending the values to the QML neither changing the angles on the js script... I expected something easy and common but is being impossible.
-
The best way to interact between C++ and QML is to make a class that inherits QQuickItem and create Q_PROPERTY-es in it, then register it in your main (or somewhere else) and import it into QML. For example:
class TestIntegration : public QQuickItem { Q_OBJECT Q_PROPERTY(int testProperty READ testProperty WRITE setTestProperty NOTIFY testPropertyChanged) public: TestIntegration(QQuickItem*parent=0) : QQuickItem(parent) , m_testProperty(0) {} int testProperty() const { return m_testProperty; } public slots: void setTestProperty(int testProperty) { m_testProperty = testProperty; emit testPropertyChanged; } signals: void testPropertyChanged(int testProperty); protected: int m_testProperty; }
Note that the above code can be autogenerated by QtCreator. You just start Q_PROP and it autocompletes it to the entire Q_PROPERTY asking you to set the type and the name of the property. Then you can rightclick Q_PROPERTY and in refactor submenu generate all methods and the field.
After that you just register the class in your main
int main(int argc, char**argv) { ... qmlRegisterType<TestIntegration>("YourNamespace", 1, 0, "TestIntegration"); ... }
and in QML you import it and make the object somewhere
import YourNamespace 1.0; TestIntegration { id: testIntegration }
and use the property how ever you like
randomQmlProperty = testIntegration.testProperty
or
testIntegration.testProperty = whateverYouSetFromQml
-
Cool!
I did what you explained and now i have a C++ class with some Q_PROPERTY's and some functions to modify them from C++. I did some tests to verify the connection between the QML and the new class. But now, as the instance of this class is on QML, How can i access it from C++ in order to change the properties?
-
Finally i could. I create a function in the QML file to be called from the C++ application. This is mostly the same i tryied at the begining but instead modifying the property from a inherited C++ class i was trying to change the property from an instance created with JavaScript as the "barrel" example that comes with Qt5.5 (in this case the property is change with a slider). The difference is that now when i change the property the event is triggering and before not, i don't know the reason...
Thanks brcha for the help!
-
Glad to be of assistance :)
I don't know what your project is like. For the most part, I use QML for UI and one (or more) similar integration classes for all the C++ code. Therefore, my main loop is in QML and it calls C++ through the integration class(es), just like I would in the widget-based Qt app call methods when a specific menu item was clicked or something similar. Thus my whole C++ part of the application is instantiated from QML, and it can have signals, slots, trees of new classes and everything else bellow the "main" C++ entry point object.
-
In my case is opposite. I have a C++ project and now I want to display a 3D model rotating depending on the moving of some sensors. So as i never used QML before i integrated in my old form a Widget container with a QQuickView inside to show the model. I took some json models from the examples to make it working, and actually i have my own designed model in 3Dstudio and Solidworks to replace the sample, but i'm "googling" in order to make that step between, seems not trivial...