MessageDialog usage summary
I. Overview
The most basic use case of MessageDialog is to pop up an alert box. It also allows users to respond in various ways depending on which buttons are enabled . The dialog is initially invisible. You need to first set the property as desired, then set visible to true or call open().
There may be several handlers depending on which standard buttons the dialog has and the ButtonRole of each button. For example, when the user presses the Cancel, Close or Abort button, the onRejected handler will be called.
A MessageDialog window is automatically temporary with respect to its parent window. So, whether you declare the dialog in the project or in the window, the dialog will be displayed in the center of the window containing the project or the window you declare it in.
The implementation of MessageDialog will be a platform message dialog, if possible. If this fails, then it tries to instantiate a QMessageBox. If that fails too, then it falls back to the QML implementation, defaultmessagdialog.QML.
In this case, you can customize the appearance by editing this file. DefaultMessageDialog. The qml contains a rectangle to hold the content of the dialog, because some embedded systems do not support multiple top-level windows. When the dialog becomes visible, it is automatically wrapped in a window if possible, or simply re-parents on top of the main window if there can only be one.
Two, use
1. Example 1
Here's a minimal example that displays an error after the user responds:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: button1
x: 270
y: 335
text: qsTr("打开消息对话框")
onClicked: {
messageDialog.open();
}
MessageDialog {
id: messageDialog
title: "标题"
icon: StandardIcon.Critical
text: "文本内容"
onAccepted: {
console.log("And of course you could only agree.")
}
}
}
}
2. Example 2
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")
}
3. Common properties
icon : QQuickStandardIcon::Icon
- icon
icon | value | Function |
---|---|---|
no icon | StandardIcon.NoIcon | for an undecorated text warning. |
StandardIcon.Question | Used to ask questions during normal operations. | |
StandardIcon.Information | Used to report normal operation information. | |
StandardIcon.Warning | Used to report non-critical errors. | |
StandardIcon.Critical | Report critical errors. |
standardButtons : StandardButtons
- This is the configuration button and the like. You can refer to the usage in Dialog l. The functions and signals in it are the same as this one.