Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Qt 5.8 : Main menu items disabled in OSX

    General and Desktop
    6
    15
    5689
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mpergand last edited by mpergand

      Hi @SGaist

      I'm on OSX 10.9.5 Mavericks
      I'm using Qt since 5.3 and manege the main menu in the same maner without troubles.
      In 5.8 it doesn't work any more !

      I construct the Quit menu like this:

      action=menu->addAction(tr("Quit"));
       action->setMenuRole(QAction::QuitRole);
       action->setShortcut(QKeySequence::Quit);
      

      If I comment the line action->setMenuRole(QAction::QuitRole);
      It becomes active again !
      But that doesn't work for About and About Qt, if you don't specify the role this two menus don't appear.

      The Apple menu is totally broken in Qt 5.8
      I'm looking to use real native Cocoa menu, if it's possible ...

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Can you check if it still happening with 5.9 ?

        And again, can you give a complete minimal example ? That way it makes it possible to reproduce the problem the same way it happens to you.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • M
          mpergand last edited by mpergand

          @SGaist said in Qt 5.8 : Main menu items disabled in OSX:

          Can you check if it still happening with 5.9 ?

          And again, can you give a complete minimal example ? That way it makes it possible to reproduce the problem the same way it happens to you.

          Here it is:

          #include <QApplication>
          #include <QMainWindow>
          #include <QMenu>
          #include <QMenuBar>
          
          class Application : public QApplication
          {
              public:
          
                  Application( int &argc, char **argv ) : QApplication(argc, argv) {}
          
                  QMenuBar* createMenuBar()
                      {
                      QMenuBar* menuBar=new QMenuBar(0);
                      QMenu* menu=menuBar->addMenu("File");	// menu Fichier
                      QAction* action=menu->addAction("Quit");
                      action->setShortcuts(QKeySequence::Quit);
                      action->setMenuRole(QAction::QuitRole);
                      connect(action, SIGNAL(triggered()), this, SLOT(quit()));
                      return menuBar;
                      }
          };
          
          
          int main(int argc, char *argv[])
          {
              Application app(argc, argv);
              app.setQuitOnLastWindowClosed(false);
              
              // create a menubar for OSX only, when no window was opened
              // here is the bug with Qt 5.8
              // if I comment this line the Quit menu is disabled in 5.6/5.7
              // in 5.8 it's always disabled as soon as  a window is closed
              app.createMenuBar();
          
              QMainWindow* win=new QMainWindow;
              win->setAttribute(Qt::WA_DeleteOnClose);
              win->setMenuBar(app.createMenuBar());   // create menubar for this window
              win->show();
          
              return app.exec();
          }
          
          1 Reply Last reply Reply Quote 0
          • M
            mpergand last edited by mpergand

            Well, there's a mess with the parentless menuBar, you need to recreate it each time you create a new window:

            int main(int argc, char *argv[])
            {
                Application app(argc, argv);
                app.setQuitOnLastWindowClosed(false);
            
                // create a menubar for OSX only, when no window was opened
                app.createMenuBar();
            
                QMainWindow* win=new QMainWindow;
                win->setAttribute(Qt::WA_DeleteOnClose);
                win->setMenuBar(app.createMenuBar());   // create menubar for this window
                win->show();
                
                app.createMenuBar();  // recreate
            
                QMainWindow* win2=new QMainWindow;
                win2->setAttribute(Qt::WA_DeleteOnClose);
                win2->setMenuBar(app.createMenuBar());   // create menubar for this window
                win2->show();
                
                app.createMenuBar(); // recreate
            
                return app.exec();
            

            }

            Now the quit menu is active in all cases: if you close the first window or the two windows.

            Regardless this bug, the submenu issue seems unrelated, if I add the following in the createMenuBar:

            action=menu->addAction(tr("Open Recent"));
            action->setMenu(new QMenu(menuBar));
            action->menu()->setEnabled(true);
            

            The submenu Open Recent is disabled.
            In Qt 5.6.1/5.7.1 it's enabled.

            Referring to the bug report QTBUG-54845, it seems the patch was not apply to 5.8, right ?

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              From the bug report the fix has been applied to 5.6.2 so it's also valid for further versions of Qt.

              It seems we have here something new.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 1
              • A
                Allactaga last edited by

                I guess I have the same or very similar problem. During my research I have found a few interesting examples. And while I am unable to reproduce in example disabled submenu items, I think it still may be useful. I had disabled submenu items in my code, just can't reproduce here, my application code is huge.
                It seems like a problem is in application initialization order.
                This works well:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QWidget w;
                    w.setContextMenuPolicy(Qt::CustomContextMenu);
                    w.show();
                
                    QMenuBar *m = new QMenuBar(nullptr);
                    QMenu *edit_menu = m->addMenu("Edit");
                    edit_menu->addAction("One");
                
                    QMenu *file_menu = m->addMenu("File");
                    file_menu->addAction("File One");
                
                    QMenu *sub_menu = new QMenu("SubMenu", file_menu);
                    sub_menu->addAction("File Two");
                
                    QAction *another_sub_menu_action = new QAction("SubMenu2", file_menu);
                    another_sub_menu_action->setMenu(sub_menu);
                
                    file_menu->addAction(sub_menu->menuAction());
                
                    return a.exec();
                }
                

                This works well too. Except for some reason you need to call context menu twice to make code work (I have used context menu signal as some kind of delayed signal after application is shown):

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QWidget w;
                    w.setContextMenuPolicy(Qt::CustomContextMenu);
                    w.show();
                
                    QMenuBar *m = new QMenuBar(nullptr);
                    QMenu *edit_menu = m->addMenu("Edit");
                    edit_menu->addAction("One");
                
                    QWidget::connect(&w, &QWidget::customContextMenuRequested, &w, [&w, m] () {
                        QMenu *file_menu = m->addMenu("File");
                        file_menu->addAction("File One");
                
                        QMenu *sub_menu = new QMenu("SubMenu", file_menu);
                        sub_menu->addAction("File Two");
                
                        QAction *another_sub_menu_action = new QAction("SubMenu2", file_menu);
                        another_sub_menu_action->setMenu(sub_menu);
                
                        file_menu->addAction(sub_menu->menuAction());
                    });
                
                    return a.exec();
                }
                

                And this is where troubles begin:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QWidget w;
                    w.setContextMenuPolicy(Qt::CustomContextMenu);
                    w.show();
                
                    QMenuBar *m = new QMenuBar(nullptr);
                    QMenu *edit_menu = m->addMenu("Edit");
                    edit_menu->addAction("One");
                
                    QTimer::singleShot(0, &w, [&w, m] () {
                        QMenu *file_menu = m->addMenu("File");
                        file_menu->addAction("File One");
                
                        QMenu *sub_menu = new QMenu("SubMenu", file_menu);
                        sub_menu->addAction("File Two");
                
                        qDebug()<<w.isVisible();
                
                        QAction *another_sub_menu_action = new QAction("SubMenu2", file_menu);
                        another_sub_menu_action->setMenu(sub_menu);
                
                        file_menu->addAction(sub_menu->menuAction());
                    });
                
                    return a.exec();
                }
                

                Take a note: in the last example I use initialization on timer timeout to add items to menu after exec() called. File menu will not appear until you click outside of the window to deactivate it and activate window again.
                Possible by the same reason in this example after clicking twice you won't see Emoji and Dictation in Edit menu:

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    QWidget w;
                    w.setContextMenuPolicy(Qt::CustomContextMenu);
                    w.show();
                
                    QMenuBar *m = new QMenuBar(nullptr);
                
                    QWidget::connect(&w, &QWidget::customContextMenuRequested, &w, [&w, m] () {
                        QMenu *edit_menu = m->addMenu("Edit");
                        edit_menu->addAction("One");
                
                        QMenu *sub_menu = new QMenu("SubMenu", edit_menu);
                        sub_menu->addAction("Two");
                
                        edit_menu->addAction(sub_menu->menuAction());
                    });
                
                    return a.exec();
                }
                

                Looks like something is not properly updated. I was trying to find a problem but haven't for now. Hope this will help to fix it because this undefined behavior is really creepy.

                1 Reply Last reply Reply Quote 0
                • P
                  patrik08 last edited by patrik08

                  No... it not a bug.... i study source code -> QtCreator last week... i build self on my box... its only a Info.plist reference after you have 1000 submenu & searh in submenu...

                  Pic from QTCreator build on my mac box...

                  0_1503944986018_small34.png

                  1 Reply Last reply Reply Quote 0
                  • B
                    brktim last edited by

                    I am not sure what the status of this problem is, but I also have this issue.

                    Here is a minimal example:

                    mac-x86:menu steffen$ cat menu.pro
                    TARGET = menu

                    CONFIG += qt
                    QT += widgets

                    SOURCES = menu.cc

                    mac-x86:menu steffen$ cat menu.cc
                    #include <QAction>
                    #include <QApplication>
                    #include <QMainWindow>
                    #include <QMenu>
                    #include <QMenuBar>

                    int main(int argc, char **argv)
                    {
                    QApplication app(argc, argv);
                    QMenuBar menuBar(0);
                    QMenu menu(&menuBar);
                    menu.setTitle("Project");
                    menuBar.addMenu(&menu);
                    QAction open(&menu);
                    open.setText("Open");
                    menu.addAction(&open);
                    QMenu sub(&menu);
                    open.setMenu(&sub);
                    QAction demo(&sub);
                    demo.setText("Demo");
                    sub.addAction(&demo);
                    QMainWindow win;
                    win.show();
                    app.exec();
                    }

                    When compiling this with Qt 5.7 on Mac OSX 10.12.6 (Sierra), everything works correctly and the "Project - Open" menu is enabled (not ghosted) and I can select its sub menu item "Demo".

                    But when compiling this with Qt 5.9.1, the "Project - Open" menu item is disabled (ghosted) and when hovering over the item, the sub menu does not pop up and I can't select the "Demo" sub-menu item.

                    So, either there is something wrong with how I build the menu tree, or something got broken in the OSX version in 5.8 or 5.9. On other systems (Windows, Linux) this problem does not happen.

                    A 1 Reply Last reply Reply Quote 0
                    • A
                      Allactaga @brktim last edited by Allactaga

                      @brktim Try to create QMainWindow object first. And then - QMenuBar. I think the bug may be somewhere in objects re-parenting.
                      upd. 5.8 version menu works much better than 5.9.0.

                      B 1 Reply Last reply Reply Quote 0
                      • B
                        brktim @Allactaga last edited by

                        @brktim Try to create QMainWindow object first. And then - QMenuBar. I think the bug may be somewhere in objects re-parenting.

                        No change.

                        1 Reply Last reply Reply Quote 0
                        • B
                          brktim last edited by

                          Filed as https://bugreports.qt.io/browse/QTBUG-63866

                          1 Reply Last reply Reply Quote 0
                          • K
                            kenchan last edited by kenchan

                            Hello @brktim I think it works if you do it this way. Does this not work for you?

                            int main(int argc, char **argv)
                            {   
                                QApplication app(argc, argv);
                                QMainWindow win;
                                QMenu menu(win.menuBar());
                                menu.setTitle("Project");
                                QAction open(&menu);
                                open.setText("Open");
                                menu.addAction(&open);
                                QMenu sub(&menu);
                                open.setMenu(&sub);
                                QAction demo(&sub);
                                demo.setText("Demo");
                                sub.addAction(&demo);
                                win.menuBar()->addMenu(&menu);
                                win.show();
                                app.exec();
                            }
                            
                            1 Reply Last reply Reply Quote 1
                            • M
                              mpergand last edited by

                              @kenchan
                              Yes it works !

                              So i look at my code and change:

                              action=menu->addAction("Open Recent");
                              action->setMenu(new QMenu(menuBar));
                              

                              to

                              action=new QAction("Open Recent");
                              action->setMenu(new QMenu(menuBar));
                              menu->addAction(action);
                              

                              Then the submenu is ok !

                              Anyway it doesn't resolve the Quit menu issue, if I close all the windows, the quit item in the Apple menu is disabled and you can't quit the app even with the shortcut.
                              The only way is to click on the quit menu through the Dock.

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post