显示消息对话框
Show message dialog
是否可以在用户点击 QML 中的按钮时显示 QML 对话框 window?
示例:
当用户在菜单栏中单击 帮助 -> 关于 时,应显示关于对话框:
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import "components"
ApplicationWindow {
id: window
visible: true
width: 1040
height: 480
aboutDlg: aboutDialog {} // does not work...
menuBar: MenuBar {
Menu {
title: qsTr("Help")
MenuItem {
text: qsTr("About")
onTriggered: aboutDlg.open();
}
}
...
components/AboutDialog.qml
import QtQuick 2.2
import QtQuick.Dialogs 1.1
MessageDialog {
id: aboutDialog
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()
}
}
当我删除行 boutDlg: aboutDialog {} // does not work...
时,单击“关于”菜单项时会报告以下错误:
qrc:/main.qml:61: ReferenceError: aboutDlg is not defined
我怎样才能做到这一点?
是否可以在用户点击 QML 中的按钮时显示 QML 对话框 window?
示例:
当用户在菜单栏中单击 帮助 -> 关于 时,应显示关于对话框:
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import "components"
ApplicationWindow {
id: window
visible: true
width: 1040
height: 480
aboutDlg: aboutDialog {} // does not work...
menuBar: MenuBar {
Menu {
title: qsTr("Help")
MenuItem {
text: qsTr("About")
onTriggered: aboutDlg.open();
}
}
...
components/AboutDialog.qml
import QtQuick 2.2
import QtQuick.Dialogs 1.1
MessageDialog {
id: aboutDialog
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()
}
}
当我删除行 boutDlg: aboutDialog {} // does not work...
时,单击“关于”菜单项时会报告以下错误:
qrc:/main.qml:61: ReferenceError: aboutDlg is not defined
我怎样才能做到这一点?