[Interest] QActions not moved to application menu properly

Ben Haller bhaller at mac.com
Tue Sep 22 20:44:03 CEST 2020


A little followup; sorry, this should have been  in my first email.  It would probably be useful to show more code; here’s the method where I create the global menu bar:

 void QtSLiMAppDelegate::makeGlobalMenuBar(void)
{
#ifdef __APPLE__
    if (!windowlessMenuBar)
    {
        windowlessMenuBar = new QMenuBar(nullptr);
        QMenu *fileMenu = new QMenu("File", windowlessMenuBar);
        
        windowlessMenuBar->addMenu(fileMenu);
        
        fileMenu->addAction("About SLiMgui", this, &QtSLiMAppDelegate::dispatch_about);
        fileMenu->addAction("Preferences...", this, &QtSLiMAppDelegate::dispatch_preferences, Qt::CTRL + Qt::Key_Comma);
        fileMenu->addSeparator();
        fileMenu->addAction("New", this, &QtSLiMAppDelegate::dispatch_newWF, Qt::CTRL + Qt::Key_N);
        fileMenu->addAction("New (nonWF)", this, &QtSLiMAppDelegate::dispatch_newNonWF, Qt::CTRL + Qt::SHIFT + Qt::Key_N);
        fileMenu->addAction("Open...", this, &QtSLiMAppDelegate::dispatch_open, Qt::CTRL + Qt::Key_O);
        fileMenu->addSeparator();
        fileMenu->addAction("Quit SLiMgui", this, &QtSLiMAppDelegate::dispatch_quit, Qt::CTRL + Qt::Key_Q);
    }
#endif
}

This is more minimal than the working version of the code I’m using, but this minimal code still reproduces the bug, for me: the about, preferences, and quit item disappear from the File menu, and are not added to the application menu.  Note that the other menu items present (“New”, etc.) remain in the File menu and work fine, and appear as expected when the app has no main window open.  Note also that if I then create a new window (by selecting “New”), the app switches back to the menu bar coded in the .ui file for the main window, and the Quit menu item, and others, are back in the application menu where they belong.  So there is something wrong specifically with the repositioning of menu items in the global menu bar.

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University


> On Sep 22, 2020, at 2:24 PM, Ben Haller <bhaller at mac.com> wrote:
> 
> 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
> 
> 



More information about the Interest mailing list