Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Accessing ContextProperty from cpp

    QML and Qt Quick
    3
    9
    1587
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ODБOï
      ODБOï last edited by

      Hello !

      Im having troubles to access an object that i putted in my rootContextProperties like this :

      /Main.cpp/
      MyUaClient client;
      engine.rootContext()->setContextProperty("client",&client);
      /************/

      -Now from my QML files i can access 'client' writing : client.name / client.age...

      -But how to access that client instance from an other C++ class ?

      Thx in advance for help,

      Levon

      raven-worx E 2 Replies Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @ODБOï last edited by raven-worx

        @LeLev said in Accessing ContextProperty from cpp:

        -But how to access that client instance from an other C++ class ?

        the same way you've set it.
        Means there is also an according getter method available. You can find it in the docs.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        ODБOï 1 Reply Last reply Reply Quote 0
        • ODБOï
          ODБOï @raven-worx last edited by ODБOï

          Hi @raven-worx thx for the answer ,

          I am looking at 'http://doc.qt.io/qt-5/qqmlcontext.html', I can not find anything... Could you please send me a link ?

          This is my class where I want to access the rootContextProperty :

          class MyUaCallback : public QObject
          {
          Q_OBJECT
          private :
          QUrl mainUrl = "main.qml";

          public:
          MyUaCallback();

          /* "static void callback" is the function witch will modify context property */

          static void callback(UA_UInt32 handle, UA_DataValue *value, void* context,QUrl url){
              QQmlApplicationEngine* engine = new QQmlApplicationEngine(url);
              //QQmlContext* context = engine->rootContext();
          
              MyUaClient *cl;
              cl = engine->rootObjects()[0];
          
              cl->setPlcString1("newStringValue"); // "setPlcString1" is method of 'MyUaClient'
                  qDebug()<< "ContextProperty modified";
          
          }
          

          };

          But this is not working

          Thx for help,
          Levon

          raven-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @ODБOï last edited by

            @LeLev said in Accessing ContextProperty from cpp:

            I am looking at 'http://doc.qt.io/qt-5/qqmlcontext.html', I can not find anything... Could you please send me a link ?

            Really?! Come on.... QQmlContext::contextProperty()
            When you would have at least tried to look over the method summary on the top of the page you would for sure had found it....

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            ODБOï 1 Reply Last reply Reply Quote 0
            • ODБOï
              ODБOï @raven-worx last edited by

              @raven-worx thx. Sorry i was looking for something starting with 'get' ...

              1 Reply Last reply Reply Quote 0
              • E
                Eeli K @ODБOï last edited by

                @LeLev Just like any other object in any C++ object? You have client available in main.cpp so you can do e.g.

                MySomethingOther otherObject;
                MyUaClient client;
                otherObject->setClient(&client);
                // set as context property...
                

                or

                MyUaClient client;
                MySomethingOther* otherObject = new MySomethingOther(&otherObject);
                // set as context property...
                

                To have access to an object which have been set as a context property you don't need to use the qml engine or context.

                But after seeing your second post I'm confused about what you actually meant and need...

                ODБOï 1 Reply Last reply Reply Quote 0
                • ODБOï
                  ODБOï @Eeli K last edited by

                  Hello @Eeli-K ,

                  As i said in the first post, i have a 'client' object instance that i have putted in my rootContextProperties like this :

                  //
                  MyUaClient client;
                  engine.rootContext()->setContextProperty("client",&client);
                  /
                  /
                  I need this to access my client member variables from QML. OK.

                  Now i just want to modify my clients member variables from another c++ class, so i need to get that particular client instance..

                  Is this more clear ?

                  Thx

                  E 1 Reply Last reply Reply Quote 0
                  • E
                    Eeli K @ODБOï last edited by Eeli K

                    @LeLev I believe it's clear, and as I said, it's just another C++ object for your other C++ object and you can bypass the qml engine and contexts altogether. I, for example, have this code:

                    Tools* tools{new Tools()};
                    ...
                    QQmlApplicationEngine* engine{new QQmlApplicationEngine()};
                        QQmlContext* ctx{engine->rootContext()};
                        Connection* conn{new Connection()};
                        ProfileData* data{new ProfileData(ctx, conn)};
                        Monitoring* monitoring{new Monitoring(conn)};
                        CameraController* camera{new CameraController(conn)};
                    
                        conn->getNetworks();
                    
                        qmlRegisterUncreatableType<CameraController>("eu.vpsystems.software", 1, 0, "CameraController", "Not to be created as QML type");
                        ctx->setContextProperty("qApplication", app);
                        ctx->setContextProperty("profileData", data);
                        ctx->setContextProperty("connection", conn);
                        ctx->setContextProperty("tools", tools);
                        ctx->setContextProperty("monitoringData", monitoring);
                        ctx->setContextProperty("camera", camera);
                        engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
                    

                    Notice how connection is used in the constructors of other objects; it's just a member object for those objects. Each object can be used in QML. The root context is passed to one object because it sets other dynamically created objects as context properties. I don't need to get any object from the root context.

                    ODБOï 1 Reply Last reply Reply Quote 1
                    • ODБOï
                      ODБOï @Eeli K last edited by

                      @Eeli-K Thx for help, I know I will be back ^^
                      sorry im beginner in Qt and not pro in c++

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post