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. Modal qml dialog from c++ with QT 5.7

Modal qml dialog from c++ with QT 5.7

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.7k 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.
  • L Offline
    L Offline
    Locate
    wrote on last edited by p3c0
    #1

    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: ""
    
    
        contentItem:
            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->setParent(itm);
    
        _dlgItem->setProperty("name", _name);
        _dlgItem->setProperty("password", _password);
    }
    

    _engine is a pointer to my QQmlApplicationEngine instance.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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);
      

      157

      1 Reply Last reply
      1
      • L Offline
        L Offline
        Locate
        wrote on last edited by
        #3

        Thanks, that solved my Problem.

        1 Reply Last reply
        0

        • Login

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