[SOLVED] How do I show a message box with Qt Quick Controls?

Issue

What is the equivalent of QMessageBox::information() when one wishes to write a QML application using Qt Quick Controls?

Solution

In Qt 6.3 and later you can use MessageDialog from QtQuick.Dialogs:

MessageDialog {
    text: "The document has been modified."
    informativeText: "Do you want to save your changes?"
    buttons: MessageDialog.Ok | MessageDialog.Cancel

    onAccepted: Qt.quit()
}

In Qt 6.2 and earlier you can use MessageDialog from Qt.labs.platform (using the same example code as above).

In Qt 5 you can use MessageDialog from QtQuick.Dialogs 1.x:

import QtQuick 2.2
import QtQuick.Dialogs 1.1

MessageDialog {
    id: messageDialog
    title: "May I have your attention please"
    text: "It's so cool that you are using Qt Quick."
    onAccepted: {
        console.log("And of course you could only agree.")
        Qt.quit()
    }
    Component.onCompleted: visible = true
}

Answered By – Zmey

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *