View Full Version : qmlRegistertype or SetContextProperty

15th September 2016, 16:32

I am trying to link the .qml file to .h/.cpp files to access the methods.I found in the example, in main.cpp 1 and 2 is written in two different sample projects. which will be the better for mobile apps.

Can anyone explain the difference between :

QQmlContext *context = appEngine.rootContext();
XmlCtlr *xmlctlr = new XmlCtlr();
context->setContextProperty("xmlctlr", xmlctlr);


qmlRegisterType<QTQCPPXMLIntegrationV1>("ABC.QTQCPPXMLIntegrationV1", 1, 0, "QTQCPPXMLIntegrationV1");

Which will be the better to map the .h/.cpp to .qml file?

16th September 2016, 10:03
The former exposes a single object created in C++ into QML context. The latter allows you to declare instances of QTQCPPXMLIntegrationV1 directly in QML.

There is no "better" or "worse" here, it depends on what you want to achieve.

16th September 2016, 16:06
Hi wysota,

Lets say, user will login into the app and he will view the list of maps. The user information like username and last login, will be passed to c++ method and store it in the qsettings.
So the list.cpp will return the list of maps and appset.cpp will handles the user information. Means one qml [main.qml] and 2 cpp [maplist.qml and appset.cpp]. Now I have to use the both cpp file in one qml.

In this case which one will be better? or should I

QQmlContext *context = appEngine.rootContext();
maplist*maplist= new maplist();
context->setContextProperty("maplist", maplist);

QQmlContext *contextUser = appEngine.rootContext();
appset*appset= new appset();
contextUser ->setContextProperty("appset", appset);

16th September 2016, 17:41
If you only need one object of each class go for the setContextProperty() approach.