Reputation: 855
I am developing Qt app for macOS.
How could I add items to menuApp? I mean the entry which appears on the menu bar right to Apple's Icon.
How can I achieve that in Qt?
I am using Qt 5.9. Qt Widgets.
menuApp is a menu which Apple puts by default in every application. In the image attached, it corresponds to a menu entry saying "Finder":
Upvotes: 6
Views: 1041
Reputation: 31
Create a MenuBar: https://doc.qt.io/qt-5/qmenubar.html.
For does who want in qml, just do this:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import Qt.labs.platform 1.1 // comment this import to show menu in your app
ApplicationWindow {
width: 640
height: 480
visible: true
title: "Simple Test"
color: "#fff"
MenuBar {
id: menuBar
Menu {
id: fileMenu
title: qsTr("File")
MenuItem { text: qsTr("&New...") }
}
Menu {
id: editMenu
title: qsTr("&Edit")
// ...
}
Menu {
id: viewMenu
title: qsTr("&View")
// ...
}
Menu {
id: helpMenu
title: qsTr("&Help")
// ...
}
}
}
doc: https://doc.qt.io/qt-5/qml-qt-labs-platform-menubar.html
Don't forget to add Widgets(in settings like Qt += or find_package) and QApplication(in main.cpp).
Upvotes: 1
Reputation: 73
As per the QT docs for menu role, the short answer is: set the menuRole
of the QMenu item QAction
to a predefined role listed in the docs, or the general QAction::TextHeuristicRole
. Some roles are attributed automatically based on the text of the menu, but otherwise you have to do it yourself.
You can either do this programmatically, or using the ui editor. Here are some images from the ui editor in QT Creator, editing my mainwindow.ui file. I have some generic "General" QMenu which is not meant to be visible, under which the QAction elements are About
which is auto-extracted and put under the menuApp as its name matches a predefined role. For the "Check for updates" QAction which matches no predefined role, I have manually set its property menuRole
to ApplicationSpecificRole
such that it is also put under the menuApp :
mainwindow.ui menu bar showing entries in QT Creator
mainwindow.ui menu role selection in QT Creator
Upvotes: 1