6

I am developing Qt app for MacOS.

How could I add items to menuApp? I mean entry which appears on menu bar right to Apple Icon

I would like to know how to achieve that in Qt, not objective-c please

I am using Qt 5.9. It is QWidget app

Edit: This is macos question, if you don't know what menuApp is, probably you can't answer this question. It is menu which Apple puts by default in every application. In the image attached it corresponds to menu entry saying "Finder"

enter image description here

Thanks in advance

RuLoViC
  • 825
  • 7
  • 23
  • Have you read https://doc.qt.io/qt-5/qmainwindow.html which leads you to https://doc.qt.io/qt-5/qmenubar.html etc.? Qt has excellent documentation and many examples you can check out. –  Feb 28 '19 at 20:00
  • Looks like this question would greatly benefit you provided a screenshot of what you have now, and then described what more you want. If nothing else, it'd help non-OSX Qt devs to know what you are talking about... And some code would be good too, if there's anything even remotely relevant you can show. – hyde Mar 01 '19 at 07:42
  • Also, possibly relevant and important, what kind of app this is? Qt Quick or QWidgets? If QWidgets, QMainWindow or just QWidget as main window? If Qt Quick, QGuiApplication or QApplication? So on 2nd though, you really should provide a MCVE (just minimal main.cpp and possible .qml file if it's Qt Quick). Good MCVE would be the files generated when you create a project of right type with Qt Creator. – hyde Mar 01 '19 at 07:46
  • 2
    Not shure it is what you need, but setting a [MenuRole](https://doc.qt.io/qt-5/qaction.html#MenuRole-enum) might help you. Use `QAction::ApplicationSpecificRole` (or a more specific role) on actions that you want to appear in that menu – Felix Mar 01 '19 at 09:49

2 Answers2

1

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

resulting app menu, where about menu was auto attributed and check for updates was manually configured

Théophane
  • 71
  • 7
  • I tried myself at an answer intending to explain in a more visual way what the docs say, because I had the same problem when getting started. Because I didn't have the context to understand everything, I think such an answer would have helped. I hope it helps others ! – Théophane Jan 01 '22 at 15:24
1

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).

SHENOISZ
  • 31
  • 5