仅在使用Qt5在MacOS上的应用程序/桌面切换后显示菜单栏 [英] Menubar is only shown after app/desktop switch on MacOS using Qt5

查看:457
本文介绍了仅在使用Qt5在MacOS上的应用程序/桌面切换后显示菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下示例代码在启动应用程序时不会显示使用Qt 5.3.2的MacOS 10.9.5上的本机菜单.前一个菜单仍然可见,但是无法使用此工具栏执行任何操作.如果我切换到另一个应用程序或另一个桌面,则该应用程序的菜单将变得可见并且可以按预期使用.

Using the following example code the native menu on MacOS 10.9.5 using Qt 5.3.2 does not show up when starting the application. The former menu remains visible but no actions can be performed with this toolbar. If I switch to another application or to another desktop, the menu of this application becomes visible and usable as expected.

我的问题与以下问题几乎相同,但答案不适用于我的代码:

My questions is pretty much the same as the following one, but the answer does not work for my code:

Qt菜单栏未显示

这里还有另一个非常相似的问题,我已经根据建议的答案修改了我的代码,但是它也不起作用:

There is another very similar question here and I already modified my code according to the suggested answer, but it does not work either:

菜单栏未显示用于简单QMainWindow代码,Qt Creator Mac OS

#include <QtGui>
#include <QtWidgets>

class MainWindow : public QMainWindow
{
public:
    MainWindow();

private:
    void create_actions_();
    void create_menus_();
    void about_();
    void dummy_();

    QMenuBar* menu_bar_;
    QMenu* file_menu_;
    QMenu* help_menu_;
    QAction* action_about_;
    QAction* action_dummy_;
};

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

    create_actions_();
    create_menus_();
}

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

void MainWindow::create_menus_()
{
    menu_bar_ = new QMenuBar(this);

    file_menu_ = new QMenu(tr("&File"));
    file_menu_->addAction(action_dummy_);
    menu_bar_->addAction(file_menu_->menuAction());

    help_menu_ = new QMenu(tr("&Help"));
    help_menu_->addAction(action_about_);

    menu_bar_->addAction(help_menu_->menuAction());

    menu_bar_->setNativeMenuBar(true);
}

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

void MainWindow::dummy_()
{
    QMessageBox::about(this, tr("Dummy"), tr("Dummy"));
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    MainWindow main_window;
    main_window.show();

    return app.exec();
}

对于再次提出同样的问题,我感到非常抱歉,但是我不允许以新手的身份发表任何评论(坦白说很糟糕!).

I am really sorry that I bring up the same question again, but I am not allowed to make any comments as a newbie (which frankly sucks!).

我正在使用以下CMake文件来构建测试项目:

I'm using the following CMake file to build the test project:

cmake_minimum_required(VERSION 2.8.12)
project(testproject)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

find_package(Qt5Widgets)
add_executable(testapp main.cpp mainwindow.h mainwindow.cpp)
target_link_libraries(testapp Qt5::Widgets)

推荐答案

在QtCreator中启动应用程序时,我遇到了一个可见但无响应的菜单栏的问题.将焦点更改为另一个应用程序,然后再返回时,菜单栏将起作用.另外,从终端运行时也可以.我的问题是,编译后创建的Mac".app"捆绑包位于自定义目录中,因此我必须在QtCreator项目->运行->工作目录中进行设置:/my/custom/path/MyProgram.app/Contents/MacOS ,并且菜单栏工作正常.这是Qt 5.5.1和OSX 10.11.

I had the problem of a visible, but unresponsive menubar upon app startup when starting within QtCreator. When changing focus to another app, and then back, the menubar would then work. Also, it was fine immediately when run from the terminal. My problem was that the Mac ".app" bundle created after compiling was in a custom directory, so I had to set in QtCreator Project->Run->Working Dir: /my/custom/path/MyProgram.app/Contents/MacOS, and the menubar worked fine. This was Qt 5.5.1 and OSX 10.11.

这篇关于仅在使用Qt5在MacOS上的应用程序/桌面切换后显示菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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