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
Forum Updated to NodeBB v4.3 + New Features

Accessing ContextProperty from cpp

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 3 Posters 2.8k 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on 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

    raven-worxR E 2 Replies Last reply
    0
    • ODБOïO ODБOï

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #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

      ODБOïO 1 Reply Last reply
      0
      • raven-worxR 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.

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #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

        raven-worxR 1 Reply Last reply
        0
        • ODБOïO 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-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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

          ODБOïO 1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • ODБOïO ODБOï

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

              ODБOïO 1 Reply Last reply
              0
              • E Eeli K

                @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ïO Offline
                ODБOïO Offline
                ODБOï
                wrote on 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
                0
                • ODБOïO ODБOï

                  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 last edited by Eeli K
                  #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.

                  ODБOïO 1 Reply Last reply
                  1
                  • E 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ïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on 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

                    • Login

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