Menubars 在QtQuick Controls 1中存在,用来显示主窗口头部的菜单栏,但在Qt5.7的QtQuick.Controls 2.0 消失了,据说Qt5.8中会重新加入
信息来源:
http://stackoverflow.com/questions/37872504/menubars-in-qtquick-controls-2
http://blog.qt.io/blog/2016/06/16/qt-5-7-released/#comment-1197915
The ApplicationWindow
of Qt Quick Controls 2 doesn't have a menuBar
property, it has been replaced by a more customizable header
property that accepts Item
(but it doesn't accept MenuBar
anymore).
Qt Quick Controls 2 are not meant to offer a native desktop application, but are meant to offer simple, efficient and customizable components. For example in QQC2 you would use a ToolBar
or a TabBar
as the header
of an ApplicationWindow
.
Although it's not documented, it seems that just having a MenuBar
as a child of an ApplicationWindow
(in both QQC1 and QQC2) sets the native menu bar on OS X (not on Android though, and I haven't tested it on other platforms).
I asked the same question on Qt blog announcing release of Qt 5.7 and this is their answer:
http://blog.qt.io/blog/2016/06/16/qt-5-7-released/#comment-1197915
So seems that we should either wait for Qt 5.8 or clone the repo as Mitch suggested in his answer.
经过验证menubar在Qt5.8中放入 Qt.labs.platform 1.0 需要import Qt.labs.platform 1.0。
https://doc-snapshots.qt.io/qt5-5.8/qml-qt-labs-platform-menubar.html#details
Qt.labs.platform中的控件不能保证以后的版本能够持续存在和兼容,menubar被移入 Qt.labs.platform中,是因为跟平台相关,QtQuick.Controls 2.0的设计目标是实现平台无关性。
menubar在Ubuntu中使用时,菜单会出现在桌面顶层的系统菜单内,并不在自己程序的主窗口中出现,主窗口隐藏后再显示,菜单不再出现。
因此,qml设计的程序中,用到主菜单,请用如下代码,完全符合QtQuick.Controls 2.0的设计理念:
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
header:GridLayout {
width: childrenRect.width
ToolButton{
text: qsTr("File")
onClicked: menu.open();
Menu {
id: menu
title: "File"
topMargin: 30
leftMargin: 10
MenuItem {
text: qsTr("File")
onTriggered: console.log("File menu click");
}
MenuItem {text: "Cut"}
MenuItem {text: "Copy"}
MenuItem {text: "Paste"}
}
}
ToolButton{
text: qsTr("Setting")
onClicked: menu1.open();
Menu {
id: menu1
title: "File"
topMargin: 30
leftMargin: 10
MenuItem {
text: "Encrypt"
}
}
}
}
footer: TabBar {
id: tabBar
TabButton {
text: qsTr("First")
}
TabButton {
text: qsTr("Second")
}
}
}