QtQuick 2 Control Menus

How can I use Menubars in QtQuick Controls 2? It used to be (in ApplicationWindow):

menuBar: MenuBar { Menu { title: qsTr('File') MenuItem { text: qsTr('&Test') onTriggered: console.log('test') } MenuItem { text: qsTr('&Exit') onTriggered: Qt.quit(); } } } 

But after upgrading to Qt 5.7, it gives this error: Invalid property name "menuBar".(M16)

PS it was used to use the device’s own menu system, for example, in OS X it used its own top-level screen screen, in Linux and Windows it used the built-in topbar in the application bar, etc.

+7
qt qml qtquick2 qtquickcontrols2
source share
4 answers

This functionality was introduced for Controls2 in Qt 5.10. The interface is very similar, except that MenuItems have been replaced with a more universal action.

The documentation is here.

I understand this is an old question, but it can still matter to passers-by like me.

+1
source share

As GrecKo said, the desktop is not the focus of the module, and therefore you will not find the MenuBar control as part of the main import. Until recently, I used RowLayout , which contains a group of ToolButton , each of which opens a Menu to emulate a menu bar for a desktop application.

However, the Qt.labs.platform module Qt.labs.platform recently been added which adds support for built-in controls such as MenuBar . The types in this module are completely native, due to less flexibility. You can already start using them if you clone the dev branch qtquickcontrols2.git .

By the way, if you never know what an equivalent type is in Qt Quick Controls 2, there is a "Type Comparison Table" here (although it is unfortunately there is currently no MenuBar ).
+6
source share

ApplicationWindow in Qt Quick Controls 2 does not have a menuBar property, it has been replaced by a more custom <property href = "http://doc.qt.io/qt-5/qml-qtquick-controls2-applicationwindow.html#header-prop" rel = "nofollow"> header , which accepts an Item (but it no longer accepts a menuBar ).

Qt Quick Controls 2 is not designed to create your own desktop application, but is designed to create simple, efficient, and customizable components. For example, in QQC2 you should use ToolBar or TabBar as header for ApplicationWindow .

Although not documented, it seems that just having a menuBar as a child of ApplicationWindow (both in QQC1 and QQC2) sets its own menu bar in OS X (not on Android, though, and I don't know, t tested it on other platforms )

+4
source share

I asked the same question on the Qt blog announcing the 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 it seems that we should either wait for Qt 5.8 or clone the repo, as Mitch suggested in his answer.

+2
source share

All Articles