qaction不显示,Qt菜单不显示

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

#include

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值