Hi all.  I’m on Qt 5.14.2, with a Qt Widgets based app that runs on both macOS 
and Linux.  I’m having trouble getting my menu bar to work.

So that things work smoothly on Linux platforms where there is a menu bar in 
each main window, I have a menu bar in the .ui file that contains my main 
window.  That design works fine, on both platforms.  Where things have gotten 
bumpy is when I decided to try to make it so my app stays running even when the 
last document window is closed, as is fairly typical for macOS apps; this 
definitely makes sense for my app, where it would be common to close one window 
and then create a new window to continue working.  To make this happen, I call 
setQuitOnLastWindowClosed(false), at startup in main(), after creating 
QApplication and before parsing the command line and creating the initial main 
window.  This, in itself, seems to work fine; now the app doesn’t quit when the 
last window is closed.  I do this on macOS only, and the remainder of this 
question applies solely to macOS; on Linux the app quits when the last window 
is closed, and there is no problem.

Since the menu bar is owned by the main windows in my app, when the last window 
is closed the menu bar reverts to a very minimal default state; all of the 
other menus disappear.  I want to have a full menu bar (perhaps minus a couple 
of main-window-specific menus) in that mode, so I’ve been trying to get a 
“global menu bar” working, as described in the QMenuBar doc (by making a 
QMenuBar object that has no parent).  I’ve tried this a couple of different 
ways, but I can’t get it to work completely.  The problem is always that some 
of the menu items that need to be moved into the application menu on macOS 
don’t move – they disappear.  Specifically, this happens to the About, 
Preferences, and Quit menu items.  The menu bar appears fine whenever a main 
window is open; the menu bar that gets created by that .ui file seems to work 
perfectly.  But the “global” menu bar that I create myself is missing those 
items.

I’ve tried creating the global menu bar either when the last window closes 
(i.e., on demand), or at startup before the first main window is created; the 
behavior is the same either way.  I’ve tried creating the Quit menu item the 
easy way, with:

 fileMenu->addAction("Quit SLiMgui", this, &QtSLiMAppDelegate::dispatch_quit, 
Qt::CTRL + Qt::Key_Q);

or the long way, creating the QAction and then setting its shortcut, connecting 
it, and finally adding it to fileMenu; the behavior is the same either way.  
I’ve tried poking the menu bar after I’ve finished creating it, by calling 
toNSMenu(); makes no difference.  I’ve tried setting the menu role for the Quit 
item to QuitRole, before adding it to fileMenu, or leaving it as the default; 
makes no difference.

In all cases, I can output the structure of the menu bar immediately after I 
create it and I see (showing just the File menu, where the Quit item gets added 
by my code):

"File"
  "About SLiMgui"
  "Preferences..."
  ""
...
  ""
  "Quit SLiMgui"

So the items in question are present, in the File menu where I put them.  But 
then when I click on the menu bar in my app, these menu items – about, 
preferences, and quit – are not in the File menu, nor are they in the 
application menu; they’re just gone.  They have been filtered out, somewhere 
down the road.

Since this global menu bar stuff is macOS-specific code anyway, I would happily 
just add the menu items to the application menu myself, avoiding Qt’s 
relocation logic.  However, as far as I can tell the QMenuBar object that I 
create doesn’t have an application menu in it; that seems to be private.  So 
Qt’s migration code isn’t doing the right thing, and I can find no way to just 
do the right thing on my own.  I’m running out of ideas.  Is this just a bug, 
or am I doing something wrong?  If it’s a bug, is there a way to trigger the 
menu item migration myself in a way that would work?

Thanks for any help; I’m at my wit’s end.

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to