Simple question about Qt menus under OSX



  • This question is properly easy for most of you. However, I did not find an appropriate answer on the question in the Qt documentation. When Qt is building an application on OSX it is imitating the behaviour of the native OSX menu structure by creating a menu entry with the program name. How can I add menu actions under this automatically generated entry. For example if I want to put an "About"-dialog there.

    Thanks for your help

    Mike


  • You should have something like

    @
    QMenu *menu = QMenu("ProgramName");
    @

    and there you can add actions using

    @
    menu->addAction("About", ...);
    @


  • Lifetime Qt Champion

    Hi,

    There are several possibilities. For the "standard" entries like Preferences and About, you can just use actions with the right names (don't forget to translate them)

    Otherwise you can use "QAction's menu role":http://qt-project.org/doc/qt-4.8/qaction.html#MenuRole-enum if you want to add several entries i.e. under Preferences

    Hope it helps



  • Thanks both of your for your replies. However, I still didn't manage it.

    The first answer from butterface was promising. Just taking the program name to get the pointer to the menu entry. Like that:

    @QMenu *menu = QMenu("ProgramName");@

    Problem is: QMenu("ProgramName") is not returning a pointer, and my attempts to create one from it failed, as well as just using the object.

    The second hint from SGaist gave me a search direction. And the idea of giving the action a specific rule is good. But I still don't know where to add the action afterwards just setting the menu role was not enough.

    Bests

    Mike



  • Sorry, my bad. This should be

    @
    QMenu *menu = new QMenu("ProgramName");
    @

    Btw.: You will not get the menu entry generated by OSX. It is to create your own one. If you don't want that I misunderstood your post.


  • Lifetime Qt Champion

    You don't need the menu "ProgramName", this one is done for you.

    You can use About and Preferences... These two should be automatically put in the right place. The menu role is to be used if you want to add custom entries.



  • Hi,

    I had again some time to try your suggestions.

    1. With the method of creating a new Menu("Programname") I get a new pointer on a new dropdown menu, but I have to put it somewhere, right? So I put it in the menubar(). With the result, that I got a second menu with the programname. Which is fine, but it is not what I search.

    2. With the second approach using just the action with the name "About", I also do not now where to put the pointer of the action, if I want it under the program name entry. If i just add it (with addAction) to the menubar() I do not see any item. However, maybe it helps when I show how I generate the actions:

    @ // Add Aboutdialog Action
    _actionAbout = new QAction(tr("About"),this);
    _actionAbout->setStatusTip(tr("About TLab"));
    connect(_actionAbout,SIGNAL(triggered()),this,SLOT(about()));
    @

    I think is quite straight forward.....It just got into my mind, I am using Qt 5.1, maybe that is a problem that the behaviour is different......

    What do you think?

    Bests

    Mike


  • Lifetime Qt Champion

    There you have a basic setup which should be cross platform

    @
    QAction *aboutAction = new QAction(tr("&About..."), this);
    aboutAction->setStatusTip(tr("Show About dialog"));

    QAction *preferencesAction = new QAction(tr("&Preferences..."), this);
    preferencesAction->setShortcut(tr("Ctrl+,"));
    preferencesAction->setStatusTip(tr("Show preferences dialog"));

    QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAction);
    QMenu *toolMenu = menuBar()->addMenu(tr("&Tools"));
    toolMenu->addAction(preferencesAction);
    @

    The actions should be moved at the right places.



  • Hi,

    I tested the menu generation on Qt 4.8 and it is working as expected. Guess I found a bug. Which I also already "posted":https://bugreports.qt-project.org/browse/QTBUG-33226

    Thanks for your help

     Mike

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.