MenuBar Not Showing for Simple QMainWindow Code, Qt Creator Mac OS
Asked Answered
W

6

10

I have been having problems adding a menu item to the built in menu bar in a Qt desktop application. I copied the code provided in the QMainWindow class reference documentation for creating a menu to a very simple application. Unfortunately, it did not show up when the code was run. I am simply trying to add a “File” menu to the menu bar. I am running Mac OSX 10.9.3 and Qt Creator 5.3.1.

The screenshots of my code are below. I tried both the uncommented and commented code in the mainwindow.cpp source.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //myMenuBar = menuBar();
    //fileMenu = myMenuBar -> addMenu(tr("&File"));

    fileMenu = menuBar() -> addMenu(tr("&File"));

    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMenuBar* myMenuBar;
    QMenu* fileMenu;
};

#endif //MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

ComeOnMenuBar.pro

#-------------------------------------------------
#
# Project created by QtCreator 2014-08-12T02:28:33
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ComeOnMenuBar
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

Any help would be really appreciated! Thank you!

Note: I know using setNativeMenuBar(false) works, but I would like the mac os native menu bar to work: the one that is displayed in the top-most left corner.

Wellwisher answered 12/8, 2014 at 10:15 Comment(1)
Have you tried to assign an action the the menu ?Batter
S
14

I had same issued in ubuntu with python

I used menubar's setNativeMenubar method. You can find this in c++ pyqt docs.

    menu = self.menuBar()
    menu.setNativeMenuBar(False)
Sailmaker answered 10/12, 2014 at 11:52 Comment(2)
This solved the problem for me - I would recommend accepting this as answerLeptosome
Same here. Solved the problem. @Wellwisher accept this answer?Disadvantage
G
9

I know its late by 4 yrs, but i ran into same issue & spotted this on the Qt forum for ubuntu/Mac OS.

https://forum.qt.io/topic/7276/menu-not-showing-up-in-menubar/15

Add the following to your main.cpp before you declare your Mainwindow w:

QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);

In my case my main.cpp file now looks like:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AddressBook addressBook;
AddressBookController controller (&addressBook);
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar); //fix for menubar notshowing in ubuntu

MainWindow w(&controller);
w.show();
return a.exec();

}

Gunsel answered 30/5, 2018 at 9:0 Comment(1)
yes, it is 2020 and the newbies Qt tutorial at doc.qt.io/qt-5/qtwidgets-tutorials-notepad-example.html still has this as the first stumbling block for users building on MacOS. Took me by surprise. One code for all platforms as a selling point for Qt ? Not completely.Communicative
E
3

It's pretty old Qt bug on OS X. You can work with QMenu and QMenuBar by calling QMenuBar::addAction, QMenuBar::removeAction and QMenuBar::insertAction. The trick is done by calling of QMenu::menuAction method.

Check the code below:

QMenu *menu = new QMenu("First menu");
menu->addAction("item 1");
menu->addAction("item 2");
m_menuBar->addAction(menu->menuAction());

Also you can check my another answer here with code snippet ready to compile and run.

Edva answered 24/9, 2014 at 5:9 Comment(0)
V
2

The answer that has the most upvote works for Python, but question is for C++. However, the method is the same.

You can add this line on top of your constructor in mainwindow.cpp:

menuBar()->setNativeMenuBar(false);
Volcanic answered 19/4, 2018 at 12:41 Comment(0)
A
1

In order for OS X to gain control over the menu bar, you need to create the menu bar without a parent widget. This means in the mainwindow.cpp file typically, you'd have to create your menu bar in code.

This is my code that also puts in the About and Preferences menu items in the PROGRAM menu drop down as is standard on a mac:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->aboutAction = new QAction(0);
    this->aboutAction->setMenuRole(QAction::AboutRole);
    this->aboutWindow = new About();
    this->preferencesAction = new QAction(0);
    this->preferencesAction->setMenuRole(QAction::PreferencesRole);
    this->preferencesWindow = new Preferences();
    this->mainMenuBar = new QMenuBar(0);  // 0 explicitly states to create it with no parent
    this->mainMenu = new QMenu(0);        // Same here
    this->mainMenuBar->addMenu(this->mainMenu);
    this->mainMenu->addAction(this->aboutAction);
    this->mainMenu->addAction(this->preferencesAction);
    this->setMenuBar(this->mainMenuBar);
    // ...
}

Preferences and About are classes that handle their respective windows and code isn't included.

You will need to remove the menu bar in the mainwindow ui form that gets automatically generated.

Arlo answered 20/2, 2020 at 8:29 Comment(3)
The same thing happens on Kubuntu 20.10, and the solution is this.Holbert
Down voting because your answer is incomplete. You should state that if you do not specify a name for each QAction nothing will be shown in the Menu. Also, you simply grouped About and Preferences under File which is weird.Viridis
@Viridis This answer is for OS X which I explicitly stated. It puts Preferences and About Action items under the menu with the application name as well as sets keyboard shortcuts and titles for them automatically due to setting the Role. You misunderstood the solution I was providing. You can see a screenshot in another question I answered: #27841358Arlo
S
-1
menu = self.menuBar()

menu.setNativeMenuBar(False)

Scholasticism answered 20/4, 2016 at 16:19 Comment(1)
Answer could be bit more elaborative.Stinky

© 2022 - 2024 — McMap. All rights reserved.