Qt Quick - MessageDialog 消息提示框
MessageDialog 使用总结
- 一、概述
- 二、使用
-
- 1. 例子一
- 2. 例子二
- 三、常用属性
一、概述
MessageDialog 最基本的用例是弹出警告框。它还允许用户根据启用的按钮以各种方式进行响应。对话框最初是不可见的。你需要首先按需设置属性,然后将visible设置为true或调用open()。
根据对话框有哪些标准按钮以及每个按钮的ButtonRole,可能有几种处理程序。例如,当用户按下Cancel、Close或Abort按钮时,onRejected处理程序将被调用。
对于其父窗口,MessageDialog窗口是自动临时的。因此,无论您在项目中还是在窗口中声明对话框,对话框都将显示在包含项目的窗口或您声明的窗口的中央。
如果可能的话,MessageDialog的实现将是一个平台消息对话框。如果这失败了,那么它将尝试实例化一个QMessageBox。如果这也失败了,那么它将退回到QML实现,defaultmessagdialog . QML。
在这种情况下,您可以通过编辑此文件来定制外观。DefaultMessageDialog。qml包含一个矩形来保存对话框的内容,因为某些嵌入式系统不支持多个顶级窗口。当对话框变得可见时,如果可能的话,它将自动包装在一个窗口中,如果只能有一个窗口,则简单地在主窗口的顶部重新添加父元素。
二、使用
1. 例子一
下面是一个在用户响应后显示错误的最小示例:
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.3Window {visible: truewidth: 640height: 480title: qsTr("Hello World")Button {id: button1x: 270y: 335text: qsTr("打开消息对话框")onClicked: {messageDialog.open();}MessageDialog {id: messageDialogtitle: "标题"icon: StandardIcon.Criticaltext: "文本内容"onAccepted: {console.log("And of course you could only agree.")}}}
}
2. 例子二
MessageDialog {title: "Overwrite?"icon: StandardIcon.Questiontext: "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.AbortComponent.onCompleted: visible = trueonYes: console.log("copied")onNo: console.log("didn't copy")onRejected: console.log("aborted")}
三、常用属性
icon : QQuickStandardIcon::Icon
- 图标
图标 | 值 | 功能 |
---|---|---|
no icon | StandardIcon.NoIcon | 为一个无修饰的文本警告。 |
StandardIcon.Question | 用于在正常操作过程中提问。 | |
StandardIcon.Information | 用于上报正常操作信息。 | |
StandardIcon.Warning | 用于报告非关键错误。 | |
StandardIcon.Critical | 报告严重错误。 |
standardButtons : StandardButtons
- 这个就是配置按钮之类的,这个可以参考 Dialog l里面的用法,里面的函数和信号和这个是一样的