Skip to content Skip to sidebar Skip to footer

Qml Reports Referenceerror: Xyz Is Not Defined On C++ Object Added To Context

I've started learning QML recently (after trying it out a long time ago) and I'm stuck at the way Qt C++ code interacts with QML and vice versa. I have a Counter which has the foll

Solution 1:

This took me perhaps 2 hours to figure out (I posted my question when I was on the verge of commiting a suicide :D) but the answer was really obvious: how can I call for a property which hasn't been initialized yet? The solution to my problem is basically to move the setContextProperty()BEFORE I load the QML file:

// ...
QQuickView view;
Counter c;
view.rootContext()->setContextProperty("counter", &c);
view.setSource(QUrl(QStringLiteral("qrc:/main.qml")));
// ...

By doing so the property is first added to the root context of the view and after that the additional QML stuff is loaded but the counter property is still present). With the previous version of my code I was basically trying to access counter inside my QML file BEFORE I have added it as a property.

Post a Comment for "Qml Reports Referenceerror: Xyz Is Not Defined On C++ Object Added To Context"