Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access rootObject from c++ file other than main.cpp

Access rootObject from c++ file other than main.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 784 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Prasad_Socionext
    wrote on last edited by VRonin
    #1

    Hi

    I need to get access for some QML object property for example two radio button from c++ file , I know how to do it in main.cpp.

    this is the code for it

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <qqmlengine.h>
    #include <qqmlcontext.h>
    #include <qqml.h>
    #include <QQmlComponent>
    #include <QStringList>
    #include "connect_serial_port.h"
    #include "tcpip_client.h"
    #include "fileio.h"
    #include "cmd_do.h"
    #include <QDebug>
    
    //#include "ui_settings.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        qmlRegisterType<FileIO, 1>("FileIO", 1, 0, "FileIO");
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        Connect_Serial_Port connect_serial_port;
        Tcpip_Client tcpip_client;
        Cmd_Do cmd_do;
    
        //Ui_Settings ui_settings;
    
    
        QQmlContext *classContext = engine.rootContext();
        classContext->setContextProperty("connect_serial_port", &connect_serial_port);
        classContext->setContextProperty("tcpip_client", &tcpip_client);
        classContext->setContextProperty("cmd_do", &cmd_do);
        //classContext->setContextProperty("Ui_Settings",&ui_settings);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        // Step 1: get access to the root object
            QObject *rootObject = engine.rootObjects().first();
            QObject *qmlObject_serial_radio = rootObject->findChild<QObject*>("serial_radio");
            QObject *qmlObject_tcpip_radio = rootObject->findChild<QObject*>("tcpip_radio");
        // Step 2a: set or get the desired property value for the root object
            rootObject->setProperty("visible", true);
            rootObject->setProperty("width", "1200");
            qDebug() << rootObject->property("visible");
            qDebug() << qmlObject_serial_radio->property("checked");
             qDebug() << qmlObject_tcpip_radio->property("checked");
    
    
    
    
    
    
    
    
        connect_serial_port.scan_for_ports();
    
        return app.exec();
    }
    

    but how do I do the same in a different c++ file other than main.cpp.
    how t o access rootObject and all of its child?

    raven-worxR 1 Reply Last reply
    0
    • P Prasad_Socionext

      Hi

      I need to get access for some QML object property for example two radio button from c++ file , I know how to do it in main.cpp.

      this is the code for it

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <qqmlengine.h>
      #include <qqmlcontext.h>
      #include <qqml.h>
      #include <QQmlComponent>
      #include <QStringList>
      #include "connect_serial_port.h"
      #include "tcpip_client.h"
      #include "fileio.h"
      #include "cmd_do.h"
      #include <QDebug>
      
      //#include "ui_settings.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          qmlRegisterType<FileIO, 1>("FileIO", 1, 0, "FileIO");
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          Connect_Serial_Port connect_serial_port;
          Tcpip_Client tcpip_client;
          Cmd_Do cmd_do;
      
          //Ui_Settings ui_settings;
      
      
          QQmlContext *classContext = engine.rootContext();
          classContext->setContextProperty("connect_serial_port", &connect_serial_port);
          classContext->setContextProperty("tcpip_client", &tcpip_client);
          classContext->setContextProperty("cmd_do", &cmd_do);
          //classContext->setContextProperty("Ui_Settings",&ui_settings);
      
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          // Step 1: get access to the root object
              QObject *rootObject = engine.rootObjects().first();
              QObject *qmlObject_serial_radio = rootObject->findChild<QObject*>("serial_radio");
              QObject *qmlObject_tcpip_radio = rootObject->findChild<QObject*>("tcpip_radio");
          // Step 2a: set or get the desired property value for the root object
              rootObject->setProperty("visible", true);
              rootObject->setProperty("width", "1200");
              qDebug() << rootObject->property("visible");
              qDebug() << qmlObject_serial_radio->property("checked");
               qDebug() << qmlObject_tcpip_radio->property("checked");
      
      
      
      
      
      
      
      
          connect_serial_port.scan_for_ports();
      
          return app.exec();
      }
      

      but how do I do the same in a different c++ file other than main.cpp.
      how t o access rootObject and all of its child?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Prasad_Socionext
      either you pass a pointer to the engine across your cpp files.

      Or if you have a pointer to an QML item you can also do the following:

      QQmlEngine* engine = qmlEngine(itemInstancePtr);
      

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

      1 Reply Last reply
      3
      • P Offline
        P Offline
        Prasad_Socionext
        wrote on last edited by
        #3

        Hi @raven-worx

        Thanks for your reply. I am clicking a button in another panel. which should do something based on the radio button chosen in another panel.
        if i am invoking a method which accesses the property of the items in the same panel , i guess i can send the object pointer as parameters to cpp.

        but in the above case I am a bit confused how to do it.

        and One more question when I toggle the radio buttons how do I know it is updated inthe QObject tree ?
        in matlab i used to do something like this guidata(fig_hdl, handles);

        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