Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved C++ find child qml item included from other file

    QML and Qt Quick
    qml dialog findchild
    3
    4
    10350
    Loading More Posts
    • 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
      PhTe last edited by

      My problem is that i have a QML component (SettingsDialog) which is in its own file (SettingsDialog.qml) and used in my main window (Main.qml).

      Now i need to access the SettingsDialog in C++ but the findChild functions always returns NULL.

      rootWindow->findChild<QQuickItem*>("settingsDialog"); // Returns NULL
      

      The findchild call to get the TabView - it is also in the Main.qml - works.

      rootWindow->findChild<QQuickItem*>("tabView") // Returns pointer to the tab view
      

      If i start the application its all fine. I can open the dialog and it will be shown, so there the QML files seem to be ok.

      Does anyone have an idea, why only the findchild call for the dialog does not work?

      Is it because of the separate QML file?

      The Main.qml looks like:

      ApplicationWindow {
          width: 1200
          height: 800
          visible: true
          id: root
      
          // Menu, Toolbar and other stuff
      
          SettingsDialog {
              id: settingsDialog
              objectName: "settingsDialog"
              visible: false
          }
      
          TabView {
              id: tabView
              anchors.fill: parent
              objectName: "tabView"
          }
      }
      

      The SettingsDialog.qml contains

      Dialog {
          id: settingsDialog
          title: "Settings"
      
          signal saveSettings();
      
          contentItem: Rectangle {
              implicitWidth: 500
              implicitHeight: 200
      
              Button {
                  id: saveButton
                  anchors.right: parent.right
                  anchors.bottom: parent.bottom
                  anchors.rightMargin: 5
                  anchors.bottomMargin: 5
                  text: qsTr("Save")
      
                  onClicked: {
                      settingsDialog.saveSettings();
                      settingsDialog.visible = false;
                  }
              }
          }
      }
      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @PhTe last edited by

        @PhTe

        rootWindow->findChild<QQuickItem*>("settingsDialog");

        Cast to QObject instead of QQuickItem.

        157

        P 1 Reply Last reply Reply Quote 0
        • ?
          A Former User last edited by

          I don't know why but I can find the dialog only if it's inside an extra Item{}:

              QObject *someItem = engine.rootObjects().first()->findChild<QObject*>("someIdentifier");
              someItem->setProperty("color", "red");
          
          import QtQuick 2.6
          import QtQuick.Window 2.2
          import QtQuick.Controls 1.4
          import QtQuick.Dialogs 1.2
          
          Window {
              visible: true
              width: 100
              height: 100
          
              Rectangle {
                  anchors.centerIn: parent
                  color: "blue"
                  width: 50
                  height: 50
              }
          
              Button {
                  onClicked: dialog.open()
              }
          
              Item {
                  Dialog {
                      property color color: "white"
                      id: dialog
                      objectName: "someIdentifier"
                      visible: false
                      contentItem: Rectangle { color: dialog.color }
                  }
              }
          }
          
          1 Reply Last reply Reply Quote 1
          • P
            PhTe @p3c0 last edited by

            @p3c0 said:

            Cast to QObject instead of QQuickItem.

            That works. :)
            Thanks.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post