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. Cannot get pointer to qml object instance:
Qt 6.11 is out! See what's new in the release blog

Cannot get pointer to qml object instance:

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.1k Views 2 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.
  • Z Offline
    Z Offline
    zeroc8
    wrote on last edited by p3c0
    #1

    I'm trying to access an object instance that is created in QML

    main.qml:

    Window {
            id: mainwindow
            objectName: "MainWindow"
            width: 1280; height: 720
    }
    

    main.cpp:

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));;
    QObject* mainWindow = engine.rootContext()->findChild<QObject*>("MainWindow");
    if(!mainWindow) throw("cannot find MainWindow");
    

    The returned mainWindow is null, why is that? Any ideas? Thanks.

    p3c0P 1 Reply Last reply
    0
    • Z zeroc8

      I'm trying to access an object instance that is created in QML

      main.qml:

      Window {
              id: mainwindow
              objectName: "MainWindow"
              width: 1280; height: 720
      }
      

      main.cpp:

      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));;
      QObject* mainWindow = engine.rootContext()->findChild<QObject*>("MainWindow");
      if(!mainWindow) throw("cannot find MainWindow");
      

      The returned mainWindow is null, why is that? Any ideas? Thanks.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      Hi @zeroc8 Thats because the Window you are trying to find is itself a root object and not a child. Thus findChild wont work. Use rootObjects() to get the root objects and then get the first item from the list. So the following gives pointer to first window:

      QObject* mainWindow = engine.rootObjects().first(); 
      

      157

      Z 1 Reply Last reply
      0
      • p3c0P p3c0

        Hi @zeroc8 Thats because the Window you are trying to find is itself a root object and not a child. Thus findChild wont work. Use rootObjects() to get the root objects and then get the first item from the list. So the following gives pointer to first window:

        QObject* mainWindow = engine.rootObjects().first(); 
        
        Z Offline
        Z Offline
        zeroc8
        wrote on last edited by
        #3

        @p3c0
        thanks, works with an instance of a builtin textcontrol. Hoever, when I try this with my own control, I cannot get a hold of it.

        qmlRegisterType<MsgCtrol>("MyStuff",1,0,"MsgCtrol");
        QObject* msgCtrl = rootObject->findChild<QObject*>("msgctrl");
        if(!msgCtrl) throw("cannot find msgctrl");

        MsgCtrol {
        objectName: "msgctrl"
        }

        Any idea what's going on here? Thanks.

        p3c0P 1 Reply Last reply
        0
        • Z zeroc8

          @p3c0
          thanks, works with an instance of a builtin textcontrol. Hoever, when I try this with my own control, I cannot get a hold of it.

          qmlRegisterType<MsgCtrol>("MyStuff",1,0,"MsgCtrol");
          QObject* msgCtrl = rootObject->findChild<QObject*>("msgctrl");
          if(!msgCtrl) throw("cannot find msgctrl");

          MsgCtrol {
          objectName: "msgctrl"
          }

          Any idea what's going on here? Thanks.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @zeroc8 And where is this MsgCtrol instantiated in ? Can you post the code ?

          157

          p3c0P 1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zeroc8
            wrote on last edited by
            #5

            MsgCtrol is instantiated in my main.qml file:

            Window {
            id: mainwindow
            width: 1280; height: 720
            color: ctrl.background

                MsgCtrol {
                    id: ctrl
                    objectName: "msgctrl"
                    onPlayMsg: Mainjs.playMsg(message);
                }
            
            1 Reply Last reply
            0
            • p3c0P p3c0

              @zeroc8 And where is this MsgCtrol instantiated in ? Can you post the code ?

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @p3c0 Ok this looks good. Now what is rootObject ? QQmlApplicationEngine doesnot have rootObject it has rootObjects( note 's' ). Are you sure you are using QQmlApplicationEngine ?

              157

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                zeroc8
                wrote on last edited by
                #7

                qmlRegisterType<MsgCtrol>("MyStuff",1,0,"MsgCtrol");
                QQmlApplicationEngine engine;
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));;

                The MsgCtrol gets instanced fine, but I need to hook it up to some additional webhandler, that's whey I wantend to get a hold of the instance from the outside.
                The solution that works is to make the webhandler a member of MsgCtrol and hook up the connections there, but that doesn't seem right.

                p3c0P 1 Reply Last reply
                0
                • Z zeroc8

                  qmlRegisterType<MsgCtrol>("MyStuff",1,0,"MsgCtrol");
                  QQmlApplicationEngine engine;
                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));;

                  The MsgCtrol gets instanced fine, but I need to hook it up to some additional webhandler, that's whey I wantend to get a hold of the instance from the outside.
                  The solution that works is to make the webhandler a member of MsgCtrol and hook up the connections there, but that doesn't seem right.

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @zeroc8 Does it work if you have Item instead of Window as root element ?

                  157

                  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