Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing ContextProperty from cpp

Accessing ContextProperty from cpp

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 3 Posters 2.4k Views
  • 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.
  • O Offline
    O Offline
    ODБOï
    wrote on 9 Oct 2017, 10:39 last edited by
    #1

    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

    R E 2 Replies Last reply 9 Oct 2017, 11:14
    0
    • O ODБOï
      9 Oct 2017, 10:39

      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

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 9 Oct 2017, 11:14 last edited by raven-worx 10 Sept 2017, 11:14
      #2

      @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

      O 1 Reply Last reply 9 Oct 2017, 11:55
      0
      • R raven-worx
        9 Oct 2017, 11:14

        @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.

        O Offline
        O Offline
        ODБOï
        wrote on 9 Oct 2017, 11:55 last edited by ODБOï 10 Sept 2017, 11:56
        #3

        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

        R 1 Reply Last reply 9 Oct 2017, 11:58
        0
        • O ODБOï
          9 Oct 2017, 11:55

          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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 9 Oct 2017, 11:58 last edited by
          #4

          @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

          O 1 Reply Last reply 9 Oct 2017, 12:01
          0
          • R raven-worx
            9 Oct 2017, 11:58

            @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....

            O Offline
            O Offline
            ODБOï
            wrote on 9 Oct 2017, 12:01 last edited by
            #5

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

            1 Reply Last reply
            0
            • O ODБOï
              9 Oct 2017, 10:39

              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

              E Offline
              E Offline
              Eeli K
              wrote on 9 Oct 2017, 12:03 last edited by
              #6

              @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...

              O 1 Reply Last reply 9 Oct 2017, 12:14
              0
              • E Eeli K
                9 Oct 2017, 12:03

                @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...

                O Offline
                O Offline
                ODБOï
                wrote on 9 Oct 2017, 12:14 last edited by
                #7

                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 9 Oct 2017, 12:36
                0
                • O ODБOï
                  9 Oct 2017, 12:14

                  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 Offline
                  E Offline
                  Eeli K
                  wrote on 9 Oct 2017, 12:36 last edited by Eeli K 10 Sept 2017, 12:37
                  #8

                  @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.

                  O 1 Reply Last reply 9 Oct 2017, 14:35
                  1
                  • E Eeli K
                    9 Oct 2017, 12:36

                    @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.

                    O Offline
                    O Offline
                    ODБOï
                    wrote on 9 Oct 2017, 14:35 last edited by
                    #9

                    @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
                    0

                    1/9

                    9 Oct 2017, 10:39

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved