当用户点击 QML 窗口中的按钮时,是否可以显示 QML 对话框?
示例:
当用户单击菜单栏中的帮助 -> 关于时,应显示关于对话框:
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();
}
}
...
组件/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 未定义
我怎样才能做到这一点?