Difference between setContextProperty and Qml registertype.



  • Hi,

    I created an UserAccessCtlr .cpp/UserAccessCtlr .h, which has methods. This method has to call from main.qml.
    I had used to QQmlContext in the main.cpp to link the .cpp/.h files. Is it the right way, since I saw in sample, qmlRegistType.

    Option 1:
    qmlRegisterType<MapQuickView>("Esri.QTQCPPXMLIntegrationV1", 1, 0, "MapView");

    Option 2:
    QQmlContext *context = appEngine.rootContext();
    UserAccessCtlr *useraccessctlr = new UserAccessCtlr();
    context->setContextProperty("useraccessctlr",useraccessctlr);

    Query:
    What is the difference and which one i have to use it?


  • Lifetime Qt Champion

    Hi,

    Option 1 makes your type known to the QML engine and then you can instantiate new objects from that type directly in your QML code.

    Option 2 "adds" an object to the context that you can use from your QML code.

    Which one to use depends on what you want to do with that object.

    Hope it helps.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.