[Resolved] Weirdness with Mac menubars
-
I observe a strange behavior with regards to Qt 4.7.4 running on OSX Lion.
I'd like my application to have a single menubar for all windows. so, my application holds and initializes a QMenuBar object:
@this->macMenuBar = new QMenuBar(0);@
When I run my application, I see my application's name on the menu bar, right to the apple logo, with some default options like Quit, Services-> etc...
I'd like to add two new options to this menu (under my application's name). So I try to do this in the QApplication's constructor.
@this->macMenuBar->addAction(configurationAction);
this->macMenuBar->addAction(aboutMyAppAction);@This has no effect on the menu. It won't show my items, although both Actions are valid and working from the dock icon menu)
Then, I decide something that doesn't make sense at all, but works partially.
@QMenu * menu = this->macMenuBar->addMenu(("blah blah"));
menu->addAction(configurationAction);
menu->addAction(aboutMyApp);@Now my two actions, "configuration", and "about" will show and there's no trace of any menu item called "blah blah" under which those actions are supposed to appear!
Now I decide to add another line:
@menu->addAction(loginAction);@
So what Do I get now? "preferences" and "about" in the underneath my application name's top level menu item, AND a new sub-menu called "blah blah" to its right, with one item only in it, my loginAction.
I am really confused here.
Why am I not getting the "blah blah" top level menu item until I add a third action?
How can I force my "loginAction" into the first top level menu, under my application's name?
-
That's usual and expectable behavior :-)
On a menubar, you usually add menus (pop down menus), but not single actions.
On the Mac, everything is a bit special. You have the so called "unified menubar":/doc/qt-4.8/qmenubar.html#qmenubar-on-mac-os-x. Based on some string matching, items like about and configuration/settings are automatically moved to the application menu. You still need to put them into a menu though, just adding the action does not work. The menu you put that special actions into, is only displayed in case there are some menu items left to display. This is the reason that it only shows up, if you add that third action to the menu.
To force the login action into the application menu, set a "menu role":/doc/qt-4.8/qaction.html#MenuRole-enum on it:
@
loginAction->setMenuRole(QAction::ApplicationSpecificRole);
@Another tip: To be platform independent, you should arrange the menus and actions for the case that you're not on a Mac. This way you're sure that everything in the correct place on that systems that do not have the unified menu bar. I personally set the menu roles even on the quit, about and settings actions. And of course on those that I want to force in the application menu.
Hope that helped.
-
Oh well, I guess that's what Steve Jobs (RIP) meant by "Think Different" ;-)
Much thanks Volker, again. It worked, but now I came upon this:
https://bugreports.qt-project.org/browse/QTBUG-24067
as you cannot add separators into your application menu ;-) I'll track that bug for a resolution. it's still there with 4.8.0
-
... furthermore, for situations where you simply want to display an action under a regular pull-down menu, you would include the following in your Mac-specific code:
@myAction->setMenuRole(QAction::NoRole);@
If the role is not explicitly defined, the action will not appear in the menu.