[英]How to access “ApplicationWindow” reference in other QML file?
[英]Create a separate QML file for MenuBar in a ApplicationWindow
我將創建一個單獨的文件,其中包含 ApplicationWindow 中 MenuBar 的組件。 我有一個這樣的原始文件:
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: console.log("Open action triggered");
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Label {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
但是我會將兩者分開以在一個單獨的文件中制作一個更復雜的菜單,如下所示:
第一個文件:
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MyMenuBar{
}
Label {
text: qsTr("Hello World")
anchors.centerIn: parent
}
}
第二個文件:(MyMenuBar.qml)
MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("&Open")
onTriggered: console.log("Open action triggered");
}
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
我已經嘗試過這個解決方案,但是當我嘗試啟動該程序時,它說MyMenuBar is not a type
。
怎么了?
您的qml
文件沒有任何問題。 如果您將兩個文件放在同一目錄中並運行qmlscene main.qml
(考慮到main.qml
是您的第一個文件),您將獲得所需的結果。 如果你在你的項目中使用C ++代碼,你可能要添加的所有qml
文件中qrc
文件的意見建議。 如果你沒有這樣的願望,我認為你的項目文件有問題。 我建議您創建一個新的Qt Quick UI
項目並將您的第一個文件代碼放入創建的文件中,然后右鍵單擊該項目並單擊add new
Qt > QML file
並選擇Qt > QML file
並將其命名為MyMenuBar.qml
並將您的代碼放入它。 我認為這應該可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.