Qt菜单不显示 [英] Qt menubar not showing

查看:1134
本文介绍了Qt菜单不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X 10.9上使用CMake(不使用MOC)构建一个简单的C ++应用程序。



命令行。问题是菜单栏根本没有显示,终端菜单栏仍然可见,但不可点击。当我暂时切换窗口,然后回到这个应用程序的窗口,我至少看到标准的应用程序菜单与关于。 关于操作现在正在工作,并显示对话框。






  • 使用预定义的 menuBar()

  • 使用 setMenuBar / code>

  • new menuBar(0)

  • menubar-> setVisible(true)



当我检查 isVisible()它返回false,也是如果我将它设置为可见的行之前。



我想知道是否缺乏使用MOC

 <$ c $ 

c> #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)

提前多谢!

解决方案



删除 menu_bar _-> addSeparator(); 已解决问题。


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!

解决方案

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

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

这篇关于Qt菜单不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆