-1
我將在ApplicationWindow中創建一個包含MenuBar組件的單獨文件。我有一個原始文件是這樣的:在ApplicationWindow中爲MenuBar創建一個單獨的QML文件
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
。
怎麼了?
您的項目是否使用'.qrc'文件?如果是這樣,請嘗試再次運行'qmake',方法是右鍵單擊Creator中的項目並按下'運行qmake',然後重新構建您的項目。 – Mitch
@Mitch是對的。你的資源文件'.qrc'應該看起來像[this](https://gist.github.com/ftena/ef94e24d2e0727a26917)。 – Tarod