Qt World Summit: Register Today!

Modal qml dialog from c++ with QT 5.7

  • I want to invoke a modal Dialog from the c++ part of my program under QT 5.7. If there is only one Text in my qml for the Dialog, I get the Dialog. But after I will set the Dialog to modal or after inserting a second Item I get an exception (read Access Violation)

    my qml part:

    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import QtQuick.Dialogs 1.2
    Dialog {
        title: "Test dialog"
        visible: true;
        modality : Qt.ApplicationModal
        standardButtons: StandardButton.Ok
        property string name: ""
        property string password: ""
            Rectangle {
                implicitWidth: 400
                implicitHeight: 100
                Text {
                    id: userLoginText
                    text: "Please enter your login data."
                Text {
                    id: userNameText
                    anchors.top: userLoginText.bottom
                    text: "Username"
                TextField {
                    id: userNameInput
                    anchors.top: userNameText.bottom
                    focus: true
                    text: name
                    onTextChanged: name = text
                Text {
                    id: passwordText
                    anchors.top: userNameInput.bottom
                    text: "Password"
                TextField {
                    id: passwordInput
                    anchors.top: passwordText.bottom
                    echoMode: TextInput.Password
                    onTextChanged: password = text

    and my c++ part:

    void DlgUserData::Show()
        QQuickWindow *itm = qobject_cast<QQuickWindow*>(_engine->rootObjects().value(0));
        QQmlComponent component(_engine, QUrl(QStringLiteral("qrc:/camUserData.qml")));
        _dlgItem = qobject_cast<QObject*>(component.create());
        _dlgItem->setProperty("name", _name);
        _dlgItem->setProperty("password", _password);

    _engine is a pointer to my QQmlApplicationEngine instance.

  • Moderators

    @Locate You should always check if the cast was successful and that you get access to that object which in your case fails and thus memory access error.
    Instead try the following:

    QObject *obj =  engine.rootObjects().first();
    obj->setProperty("name", _name);

  • Thanks, that solved my Problem.

Log in to reply