Results 1 to 4 of 4

Thread: qmlRegistertype or SetContextProperty

  1. #1
    Join Date
    Jul 2016
    Posts
    53
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default qmlRegistertype or SetContextProperty

    Hi,

    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 :

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

    and

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


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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: qmlRegistertype or SetContextProperty

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Mathan (16th September 2016)

  4. #3
    Join Date
    Jul 2016
    Posts
    53
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: qmlRegistertype or SetContextProperty

    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);

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qmlRegistertype or SetContextProperty

    If you only need one object of each class go for the setContextProperty() approach.

    Cheers,
    _

Similar Threads

  1. QML with setContextProperty deployment
    By curtis in forum Installation and Deployment
    Replies: 2
    Last Post: 11th November 2015, 00:58
  2. Replies: 3
    Last Post: 3rd March 2015, 18:53
  3. Understanding argument `const char * uri` of `qmlRegisterType`
    By TheIndependentAquarius in forum Qt Quick
    Replies: 6
    Last Post: 23rd October 2013, 12:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.