Qt menubar not showing

I am building a simple C ++ application on Mac OS X 10.9 with Qt 5.2.1 using CMake (without MOC).

I run the executable from the command line. The problem is that the menu bar is not displayed at all, the terminal menu bar is still visible, but not accessible. When I temporarily switch windows, and then return to the window of this application, I at least see the standard "application" menu with "O". The "About" action now works and displays a dialog. The toolbar button also works as expected.

What else I tried (and did not work):

  • using predefined menuBar()
  • use setMenuBar()
  • new menuBar(0)
  • menubar->setVisible(true)

When I check isVisible(), it returns false, also if I set it visible in the string earlier.

I wonder if there could be a reason for using MOC?

Below I have attached the given example.

#include <QtGui>
#include <QtWidgets>


class MainWindow : public QMainWindow {

public:

  MainWindow();


private:

  void create_actions_();
  void create_menus_();
  void create_toolbar_();

  void about_();

  QMenuBar* menu_bar_;
  QMenu* file_menu_;
  QMenu* help_menu_;

  QToolBar* file_toolbar_;

  QAction* action_about_;

};



MainWindow::MainWindow() {
  resize(800, 600);

  create_actions_();
  create_menus_();
  create_toolbar_();
}


void MainWindow::create_actions_() {
  action_about_ = new QAction(tr("About"), this);
  connect(action_about_, &QAction::triggered, this, &MainWindow::about_);
}


void MainWindow::create_menus_() {

  menu_bar_ = new QMenuBar(this);

  file_menu_ = menu_bar_->addMenu(tr("&File"));

  menu_bar_->addSeparator();

  help_menu_ = menu_bar_->addMenu(tr("&Help"));
  help_menu_->addAction(action_about_);

  menu_bar_->setNativeMenuBar(true);
}


void MainWindow::create_toolbar_() {

  file_toolbar_ = addToolBar(tr("File"));
  file_toolbar_->addAction(action_about_);

  file_toolbar_->setIconSize(QSize(16, 16));

}


void MainWindow::about_() {
  QMessageBox::about(this, tr("About"), tr("FooBar"));
}


int main(int argc, char **argv) {

  QApplication app(argc, argv);

  MainWindow main_window;
  main_window.show();

  const int exit_code = app.exec();
  return exit_code;
}

CMakeLists.txt

FIND_PACKAGE(Qt5Core)
FIND_PACKAGE(Qt5Gui)
FIND_PACKAGE(Qt5OpenGL)
FIND_PACKAGE(Qt5Widgets)
FIND_PACKAGE(Qt5Declarative)
FIND_PACKAGE(Qt5MacExtras)

ADD_EXECUTABLE(main main.cc)
qt5_use_modules(main Core Gui Widgets Declarative MacExtras)

Many thanks!

+2
source share
1 answer

OK, I solved the problem myself. It looks like you cannot add a separator to the menu bar.

Removing menu_bar_->addSeparator();solved the problem.

+2
source

All Articles