Dialog QtQuick access problem



  • Hi to everybody,

    I've a little problem with the access to Dialog Qt Quick object from c++ side. When I try access to a Dialog object in qml I have some problem if it's not in a "Rectangle" or "Item" object.

    main.cpp
    @
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QDebug>
    #include <QObject>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    // Step 1: get access to the root object
    QObject *rootObject = engine.rootObjects().first();
    qDebug() << rootObject->objectName();
    
    QObject *qmlObj = rootObject->findChild<QObject*>("testItem");
    qDebug() << qmlObj->objectName();
    
    QObject *qmlDialog = qmlObj->findChild<QObject*>("testObj");
    qDebug() << qmlDialog->objectName();
    
    // this doesn't works    
    qmlDialog = rootObject->findChild<QObject*>("testObj2");
    qDebug() << qmlDialog->objectName();
    
    // this doesn't works
    qmlDialog = rootObject->findChild<QObject*>("testObj3");
    qDebug() << qmlDialog->objectName();
    
    return app.exec&#40;&#41;;
    

    }
    @

    main.qml
    @
    import QtQuick 2.2
    import QtQuick.Window 2.0
    import QtQuick.Dialogs 1.2
    import QtQuick.Controls 1.2

    Window {
    id: mainWindow
    objectName: "mainWindow"
    visible: true
    width: 640
    height: 480

    Item {
        id: testItem
        objectName: "testItem"
        Dialog {
            id: testObj
            objectName: "testObj"
            modality: Qt.WindowModal
    
            contentItem: Rectangle {
                Text {
                    text: "Test Obj"
                }
            }
        }
    }
    
    MessageDialog {
        id: testObj2
        objectName: "testObj2"
        text: "Test Obj"
    }
    
    Dialog {
        id: testObj3
        objectName: "testObj3"
        modality: Qt.WindowModal
    
        contentItem: Rectangle {
            Text {
                text: "Test Obj"
            }
        }
    }
    

    }

    @

    In particular it doesn't works when I try to access at testObj2 and testObj3. Why?


Log in to reply
 

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