title: Qt: Change application QMenuBar contents on Mac OS X
body: <p>My application uses a QTabWidget for multiple 'pages', where the top-level menu changes depending on what page the user is on.</p>
  
  <p>My issue is that attempting to re-create the contents of the menu bar results in major display issues. It works as expected with the first and third style (haven't tested second, but I'd rather not use that style) on all platforms except for Mac OS X.</p>
  
  <p>The first menu is created in the way I create most in the application, and they receive the correct title, but disappear as soon as the menu is re-created.</p>
  
  <p>The second menu appears both on the initial population and the re-population of the menu bar, but in both cases has the label "Untitled". The style for the second menu was only created when trying to solve this, so it's the only way I've been able to have a menu stick around. </p>
  
  <p>The third dynamic menu never appears, period. I use this style for dynamically populating menus that are about to show.</p>
  
  <p>I have tried deleting the QMenuBar and re-creating one with</p>
  
  <pre><code>m_menuBar = new QMenuBar(0);
  </code></pre>
  
  <p>and using that as opposed to <code>m_menuBar->clear()</code> but it has the same behavior.</p>
  
  <p>I don't have enough reputation to post images inline, so I'll include the imgur links:</p>
  
  <p><b>Launch behavior</b>: <a href="http://i.imgur.com/ZEvvGKl.png" rel="nofollow">http://i.imgur.com/ZEvvGKl.png</a></p>
  
  <p><b>Post button-click behavior</b>: <a href="http://i.imgur.com/NzRmcYg.png" rel="nofollow">http://i.imgur.com/NzRmcYg.png</a></p>
  
  <p>I have created a minimal example to reproduce this behavior on Mac OS X 10.9.4 with Qt 5.3.</p>
  
  <p><b>mainwindow.cpp</b></p>
  
  <pre><code>#include "mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
  {
      m_menuBar = new QMenuBar(0);
      m_dynamicMenu = new QMenu("Dynamic");
      connect(m_dynamicMenu, SIGNAL(aboutToShow()), this, SLOT(updateDynamicMenu()));
  
      changeMenuBar();
  
      QPushButton *menuBtn = new QPushButton("Test");
      connect(menuBtn, SIGNAL(clicked()), this, SLOT(changeMenuBar()));
  
      setCentralWidget(menuBtn);
  }
  
  void MainWindow::changeMenuBar() {
      m_menuBar-&gt;clear();
  
      // Disappears as soon as this is called a second time
      QMenu *oneMenu = m_menuBar-&gt;addMenu("One");
      oneMenu-&gt;addAction("foo1");
      oneMenu-&gt;addAction("bar1");
      oneMenu-&gt;addAction("baz1");
  
      // Stays around but has 'Untitled' for title in menu bar
      QMenu *twoMenu = new QMenu("Two");
      twoMenu-&gt;addAction("foo2");
      twoMenu-&gt;addAction("bar2");
      twoMenu-&gt;addAction("baz2");
      QAction *twoMenuAction = m_menuBar-&gt;addAction("Two");
      twoMenuAction-&gt;setMenu(twoMenu);
  
      // Never shows up
      m_menuBar-&gt;addMenu(m_dynamicMenu);
  }
  
  void MainWindow::updateDynamicMenu() {
      m_dynamicMenu-&gt;clear();
      m_dynamicMenu-&gt;addAction("foo3");
      m_dynamicMenu-&gt;addAction("bar3");
      m_dynamicMenu-&gt;addAction("baz3");
  }
  </code></pre>
  
  <p><b>mainwindow.h</b></p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QtWidgets&gt;
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      MainWindow(QWidget *parent = 0);
  
  private slots:
      void changeMenuBar();
      void updateDynamicMenu();
  
  private:
      QMenuBar *m_menuBar;
      QMenu *m_dynamicMenu;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>