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. How can I access my control in windows qml type from C++ while using QQmlApplicationEngine?
Forum Updated to NodeBB v4.3 + New Features

How can I access my control in windows qml type from C++ while using QQmlApplicationEngine?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 1.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.
  • Q Offline
    Q Offline
    QtBeginnnnner
    wrote on last edited by
    #1

    here is my qml file ,very simple one. I have tried two ways to get pointer to my button but failed
    Window {
    id:mainWindow
    objectName: "mainWindow"
    visible: true
    width: 480
    height: 320
    title: qsTr("Hello World")
    Button {
    id: button1
    x: 0
    y: 0
    width: 173
    height: 88
    text: qsTr("Button")
    }
    }

    first I tried :
    int main(int argc, char argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    if (engine.rootObjects().isEmpty())
    return -1;
    QObject
    rootObject = engine.rootObjects().first();
    QObject * btn = rootObject->findChild<QObject*>("Button");
    return app.exec();
    }
    but failed, btn is NULL.

    second , I tried this way( https://forum.qt.io/topic/83225/findchild-always-returns-null-looking-for-qml-objects/3 ):
    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    if (engine.rootObjects().isEmpty())
    return -1;
    QQuickWindow * rootWindow = (QQuickWindow *)engine.rootObjects().first();
    if (rootWindow)
    rootWindow->setProperty("width", 1360);
    QObject * btn = Q_NULLPTR;
    auto list = rootWindow->contentItem()->childItems();
    for (auto i : list) {
    btn = i->findChild<QObject *>("Button"); //do not find ,btn is NULL
    if (btn) break;
    }

    return app.exec();
    

    }
    but failed, btn is NULL.

    so how could I access my control defined in qml file ?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @QtBeginnnnner said in How can I access my control in windows qml type from C++ while using QQmlApplicationEngine?:

      btn = i->findChild<QObject *>("Button")

      Button {
      id: button1
      objectName : "QtButton"

      }

      Then do the following. It will work.
      btn = i->findChild<QObject *>("QtButton")

      Not sure why you are doing this. It is not good practice.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      Q 2 Replies Last reply
      1
      • dheerendraD dheerendra

        @QtBeginnnnner said in How can I access my control in windows qml type from C++ while using QQmlApplicationEngine?:

        btn = i->findChild<QObject *>("Button")

        Button {
        id: button1
        objectName : "QtButton"

        }

        Then do the following. It will work.
        btn = i->findChild<QObject *>("QtButton")

        Not sure why you are doing this. It is not good practice.

        Q Offline
        Q Offline
        QtBeginnnnner
        wrote on last edited by
        #3

        @dheerendra hi dheerendra, I have modified my code to
        Button {
        id: idButton1
        objectName: "objButton1"
        ...
        text: qsTr("txtButton1")
        }

        btn = i->findChild<QObject *>("objButton1");

        but , still got a null pointer ..
        and I'm doing this for preparation to transfer my app frame from wxwidget to QT/QML, any suggestions ?

        J.HilkJ 1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Try this

          Window {
              id:mainWindow
              objectName: "mainWindow"
              visible: true
              width: 480
              height: 320
              title: qsTr("Hello World")
              Button {
                  id: button1
                  x: 0
                  y: 0
                  width: 173
                  height: 88
                  text: qsTr("Button")
                  objectName: "pthinks.com"
              }
           }
          int main(int argc, char *argv[])
          {
              QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
          
              QGuiApplication app(argc, argv);
          
              QQmlApplicationEngine engine;
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              if (engine.rootObjects().isEmpty())
                  return -1;
              QObject *rootObj = engine.rootObjects().first();
              qDebug() << Q_FUNC_INFO << rootObj->objectName() << endl;
              foreach (auto o1, rootObj->children()) {
                  qDebug()  << o1->objectName() << endl;
              }
          
              return app.exec();
          }
          

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • Q QtBeginnnnner

            @dheerendra hi dheerendra, I have modified my code to
            Button {
            id: idButton1
            objectName: "objButton1"
            ...
            text: qsTr("txtButton1")
            }

            btn = i->findChild<QObject *>("objButton1");

            but , still got a null pointer ..
            and I'm doing this for preparation to transfer my app frame from wxwidget to QT/QML, any suggestions ?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @QtBeginnnnner
            I think you're already too deep down the children tree to find the button,
            try this:

            QQuickWindow * rootWindow = (QQuickWindow *)engine.rootObjects().first();
            auto list = rootWindow->contentItem()->childItems();
            
            for(QQuickItem *item : list)
                if(item->objectName() == "objButton1")
                    qDebug() << "Found the button";
            
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • dheerendraD dheerendra

              @QtBeginnnnner said in How can I access my control in windows qml type from C++ while using QQmlApplicationEngine?:

              btn = i->findChild<QObject *>("Button")

              Button {
              id: button1
              objectName : "QtButton"

              }

              Then do the following. It will work.
              btn = i->findChild<QObject *>("QtButton")

              Not sure why you are doing this. It is not good practice.

              Q Offline
              Q Offline
              QtBeginnnnner
              wrote on last edited by
              #6

              @dheerendra @J-Hilk
              Thank you very much.
              Finally , two ways to get pointer to my button both works , here is the code.

              import QtQuick 2.9
              import QtQuick.Window 2.2
              import QtQuick.Layouts 1.2
              import QtQuick.Controls 1.4

              Window {
              id:idWindow1
              objectName: "objWindow1"
              visible: true
              width: 480
              height: 320
              title: qsTr("Hello World")

              Button {
              	id: idButton1
              	objectName: "objButton1"
              	x: 0
              	y: 0
              	width: 173
              	height: 88
              	text: qsTr("txtButton1")
              }
              

              }

              #include "mainwindow.h"
              #include <QApplication>
              #include <QGuiApplication>
              #include <QQmlApplicationEngine>
              #include <QQuickWindow>
              #include <QPushButton>
              #include <QQuickItem>

              //method 1 OK
              int main(int argc, char *argv[]){
              QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              QGuiApplication app(argc, argv);
              QQmlApplicationEngine engine;
              engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
              if (engine.rootObjects().isEmpty()) return -1;
              QQuickWindow * rootWindow = (QQuickWindow *)engine.rootObjects().first();
              if (rootWindow)
              rootWindow->setProperty("width", 1360);
              QObject * btn = Q_NULLPTR;
              auto list = rootWindow->contentItem()->childItems();
              for (QQuickItem *item : list) {
              if (item->objectName() == "objButton1") {
              qDebug() << "Found the button";
              btn = item;
              }
              if (btn) break;
              }
              btn->setProperty("text", "textNew1");

              return app.exec();
              

              }

              //method 2 OK
              int main(int argc, char argv[])
              {
              QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              QGuiApplication app(argc, argv);
              QQmlApplicationEngine engine;
              engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
              if (engine.rootObjects().isEmpty())
              return -1;
              QObject
              rootObject = engine.rootObjects().first();
              QObject * btn = rootObject->findChild<QObject*>("objButton1");

              btn->setProperty( "text", "textNew2" );
              
              return app.exec();
              

              }

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                Don't do that.

                Here are some resources to tell you why :) :
                https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
                http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
                https://youtu.be/vzs5VPTf4QQ?t=23m20s

                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