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. C++ find child qml item included from other file
QtWS25 Last Chance

C++ find child qml item included from other file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmldialogfindchild
4 Posts 3 Posters 11.6k 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.
  • P Offline
    P Offline
    PhTe
    wrote on 23 May 2016, 10:37 last edited by
    #1

    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;
                }
            }
        }
    }
    P 1 Reply Last reply 23 May 2016, 10:52
    0
    • P PhTe
      23 May 2016, 10:37

      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;
                  }
              }
          }
      }
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 23 May 2016, 10:52 last edited by
      #2

      @PhTe

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

      Cast to QObject instead of QQuickItem.

      157

      P 1 Reply Last reply 23 May 2016, 11:39
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on 23 May 2016, 11:08 last edited by
        #3

        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
        1
        • P p3c0
          23 May 2016, 10:52

          @PhTe

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

          Cast to QObject instead of QQuickItem.

          P Offline
          P Offline
          PhTe
          wrote on 23 May 2016, 11:39 last edited by
          #4

          @p3c0 said:

          Cast to QObject instead of QQuickItem.

          That works. :)
          Thanks.

          1 Reply Last reply
          0

          4/4

          23 May 2016, 11:39

          • Login

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