Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

c++ - Qt menubar not showing

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

I am starting the executable from the command-line. The problem is that the menu bar is not showing up at all, the Terminal menu bar is still visible but not clickable. When I switch windows temporarily and then come back to the window of this application, I at least see the standard "application" menu with "About". The "About" action is now working and shows the dialog. The toolbar button also works as expected.

What else I tried (and didn't work):

  • using the pre-defined menuBar()
  • use setMenuBar()
  • new menuBar(0)
  • menubar->setVisible(true)

When I check the isVisible() it returns false, also if I set it to visible in the line before.

I wonder whether the lack of using MOC can be the reason for this?

Below I attached a reduced 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)

Thanks a lot in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK, solved the problem myself. It appears you cannot add a separator to the menubar.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...