问题
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)
回答1:
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_ );
}
回答2:
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.
来源:https://stackoverflow.com/questions/26027184/menubar-is-only-shown-after-app-desktop-switch-on-macos-using-qt5