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

前端 未结 6 1446
时光取名叫无心
时光取名叫无心 2020-12-10 15:29

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 documentat

6条回答
  •  庸人自扰
    2020-12-10 15:39

    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.

提交回复
热议问题