Menubar in MacOS not working.
-
Hey,
I am following a beginner's course on Qt and tutors usage of the menubar functions do not work for on my Mac.
I have looked on Google and the Qt documentation, but i could either not understand it, or it didn't work when i tried it. I need a really basic explanation.
The report i found here (https://bugreports.qt.io/browse/QTBUG-57072) did solve it partially, but with that i could not pass an predefined Action variable in the addAction function.
Side-question: I am also using the free Qt Creator 4.8.0 (Based on Qt 5.12.0). Is this a older version than the one in the report? (5.6) Or does the free version have another version count?The question is; how should this code be changed to get it to work with the Mac menubar?
#include "mainwindow.h" #include <QPushButton> #include <QMenuBar> #include <QStatusBar> #include <QDebug> #include <QAction> #include <QApplication> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { //Add central widget QPushButton * button = new QPushButton("Hello",this); setCentralWidget(button); //Declare Quit Action QAction * quitAction = new QAction("Quit"); connect(quitAction,&QAction::triggered,[=](){ QApplication::quit(); }); //Add menus QMenu * fileMenu = menuBar()->addMenu("File"); fileMenu->addAction(quitAction); menuBar()->addMenu("Edit"); menuBar()->addMenu("Window"); menuBar()->addMenu("Settings"); menuBar()->addMenu("Help"); } MainWindow::~MainWindow() { }
-
Hi,
What exactly does not work ?
-
@DevinQT said in Menubar in MacOS not working.:
- Side-question: I am also using the free Qt Creator 4.8.0 (Based on Qt 5.12.0). Is this a older version than the one in the report? (5.6) Or does the free version have another version count?
QtCreator 4.8 is the newest version.
They talk about Qt version 5.6, not QtCreator version.
(the" based on" in About, is the Qt version used to compile Creator)You can use QtCreator with any version of Qt, even multiple versions.
There is no difference between open source and commercial QtCreator.
-
Your menus are all empty. Add one or more actions to them and they will appear.
-
There is one action in the menu. "quitAction"
I've added another action which is not a variable, but it doesn't show up.I have taken the source code produced by the lecture and run the exact same code:
Left: my Qt window and my app. Right: the lecture.(sidenote: I have deleted the status bar tho, so not exactly the same code)
-
You need to populate the menus with some items:
QMenu * menu = menuBar()->addMenu("File"); menu->addAction(quitAction); menu=menuBar()->addMenu("Edit"); QAction* action=menu->addAction(tr("Undo")); action->setShortcut(QKeySequence::Undo); menu=menuBar()->addMenu("Window"); action=menu->addAction(tr("window 1")); menu=menuBar()->addMenu("Settings"); action=menu->addAction(tr("settings 1")); menu=menuBar()->addMenu("Help"); action=menu->addAction(tr("help me !"));
-
Thanks. That adds a few menu items. It skipped 'File' and 'Settings', but i guess there's a reason for that?
Yes there is and it's all explained in QMenuBar's documentation.
Exit and settings action are moved to the "application name" menu on macOS.
-
Working on same course, had same mac issue. Note: "quit" is not visible - by design - on a Mac. After a lot of searching, here is fix that works on both Linux and Mac:
```
// declare actions from .h
act_quit = new QAction("quit");
act_stop = new QAction("Stop");// connect actions QObject::connect(act_quit,&QAction::triggered,this, [=](){QApplication::quit(); }); QObject::connect(act_stop,&QAction::triggered,this, [=](){QApplication::quit(); }); // add menus QMenu *filemenu = menuBar()->addMenu("File"); filemenu->addAction(act_quit); filemenu->addAction(act_stop); QMenu *mnu_other = menuBar()->addMenu("Other"); mnu_other->addAction(act_quit); mnu_other->addAction(act_stop); QMenu *mnu_three = menuBar()->addMenu("hello"); mnu_three->addAction(act_quit); mnu_three->addAction(act_stop);