@Tom-asso said in Access read-only C++ property from QML:
Here is my QML:
ApplicationWindow { [...] property string testString: SharedConstants.testString Component.onCompleted: {console.log("onCompleted"); console.log("testString: ", testString)}But I will be adding other stuff to SharedConstants (e.g. enum) so I must actually register SharedConstants (as described here) in main.cpp:
// Register SharedConstants so QML can access its properties qmlRegisterType<SharedConstants>("SharedConstants", 1, 0, "SharedConstants");
Debugging shows that the designated C++ accessor function getTestString() is not invoked either.
What am I doing wrong here?
qmlRegisterType registers an instantiable type, but the QML code above attempts to use it as a singleton. Either instantiate an instance of the type in QML, or use one of the singleton registration methods such as QML_SINGLETON, qmlRegisterSingletonInstance, or qmlRegisterSingletonType.