Menubar is only shown after app/desktop switch on MacOS using Qt5
Asked Answered
A

2

4

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 menubar not showing

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

MenuBar Not Showing for Simple QMainWindow Code, 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!).

Edit: 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)
Affliction answered 24/9, 2014 at 21:54 Comment(2)
I have the similar issue, did you solve the problem?Irremeable
Me too with Qt 5.6.2 and OSX 10.11.6. Any info appreciated.Bondy
I
0

I made a couple changes to your syntax. But I think the big issue may be that you have your class implementation in the same file as main(). I believe this causes issues for the meta-code created for signals/slots mechanisms.

This works for me:

main.cpp

#include <mainwindow.h>


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

    MainWindow main_window;
    main_window.show();

    return app.exec();
}

mainwindow.h

#include <QApplication>
#include <QtGui>
#include <QObject>


class MainWindow : public QMainWindow
{
    // NOTICE THIS MACRO
    Q_OBJECT
    //

    public:
        MainWindow();

    public slots:
        void dummy();
        void about();

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

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

mainwindow.cpp

#include <mainwindow.h>

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

    create_actions_();
    create_menus_();
}

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

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

void MainWindow::create_actions_()
{
    action_about_ = new QAction( "About", this );
    action_dummy_ = new QAction( "Dummy", this );

    connect( action_about_, SIGNAL( triggered() ),
             this, SLOT( about() ) );

    connect( action_dummy_, SIGNAL( triggered() ),
             this, SLOT( dummy() ) );
}

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

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

    help_menu_ = new QMenu( "&Help" );
    help_menu_->addAction( action_about_ );
    menu_bar_->addMenu( help_menu_ );
}
Islander answered 25/9, 2014 at 0:34 Comment(6)
Thank you for you answer, but unfortunately your code is not working either. Did you test with Qt5? It seems that an include to QMainWindow is missing. In Qt5 this is located in QtWidgets and an include to QtGui is no longer sufficient.Affliction
You're right. I built this using Qt 4.8.6. That shouldn't matter though, did you copy paste and include QMainWindow? Can you link your .pro file?Islander
Can you repost that CMake? No worries on the codeblock.Islander
I attached it to my original code at the end of my question.Affliction
Using CMake I create a project for Xcode 6.0.1 and build the code with Clang.Affliction
I found a very similar report for Lyx, but the proposed fix does not work: mail-archive.com/[email protected]/msg185121.htmlAffliction
T
0

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.

Tedmann answered 23/8, 2016 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.