MessageDialog in QML



  • Hey again to all :D

    I am back with a little problem.:D
    I want to create a MessageDialog in QML.
    I have found this in Qt documentation (Qt doc).

    And if i try to run this example:

    import QtQuick 2.2
    import QtQuick.Dialogs 1.1
    
    MessageDialog {
        title: "Overwrite?"
        icon: StandardIcon.Question
        text: "file.txt already exists.  Replace?"
        detailedText: "To replace a file means that its existing contents will be lost. " +
            "The file that you are copying now will be copied over it instead."
        standardButtons: StandardButton.Yes | StandardButton.YesToAll |
            StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
        Component.onCompleted: visible = true
        onYes: console.log("copied")
        onNo: console.log("didn't copy")
        onRejected: console.log("aborted")
    }
    

    Doesn't give me any error but when i use this in main :

        QQuickView view;
        view.setSource(QUrl("qrc:/window.qml"));
        view.show();
    

    Popup a empty window.
    And what is more strange is when i run, popup tow window and i don't understand why.
    Is there another way or maybe the correct way to use MessageDialog ?



  • I think your code give you an error like this:
    "QQuickView only supports loading of root objects that derive from QQuickItem.
    Ensure your QML code is written for QtQuick 2, and uses a root that is or
    inherits from QtQuick's Item (not a Timer, QtObject, etc). "
    So you need put your MessageDialog to e.g. Item wrap like this:

    import QtQuick 2.2
    import QtQuick.Dialogs 1.1
    Item{
    MessageDialog {
        title: "Overwrite?"
        icon: StandardIcon.Question
        text: "file.txt already exists.  Replace?"
        detailedText: "To replace a file means that its existing contents will be lost. " +
            "The file that you are copying now will be copied over it instead."
        standardButtons: StandardButton.Yes | StandardButton.YesToAll |
            StandardButton.No | StandardButton.NoToAll | StandardButton.Abort
        Component.onCompleted: visible = true
        onYes: console.log("copied")
        onNo: console.log("didn't copy")
        onRejected: console.log("aborted")
    }
    }
    

    And It should work now.
    And in main use only:

    QQuickView view;
    view.setSource(QUrl("qrc:/window.qml"));
    


  • Yes, now works fine.
    Thank you @Prochy :)


Log in to reply