C++ find child qml item included from other file



  • 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;
                }
            }
        }
    }

  • Moderators

    @PhTe

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

    Cast to QObject instead of QQuickItem.


  • Moderators

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


  • @p3c0 said:

    Cast to QObject instead of QQuickItem.

    That works. :)
    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.