【问题标题】:MenuBar Not Showing for Simple QMainWindow Code, Qt Creator Mac OS简单的 QMainWindow 代码,Qt Creator Mac OS 的菜单栏不显示
【发布时间】:2014-10-05 08:56:24
【问题描述】:

在 Qt 桌面应用程序的内置菜单栏中添加菜单项时遇到问题。我将 QMainWindow 类参考文档中提供的用于创建菜单的代码复制到了一个非常简单的应用程序中。不幸的是,它没有在代码运行时出现。我只是想在菜单栏中添加一个“文件”菜单。我正在运行 Mac OSX 10.9.3 和 Qt Creator 5.3.1。

我的代码截图如下。我在 mainwindow.cpp 源代码中尝试了未注释和已注释的代码。

ma​​inwindow.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;
}

ma​​inwindow.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

ma​​in.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

任何帮助将不胜感激!谢谢!

注意:我知道使用 setNativeMenuBar(false) 可以工作,但我希望 mac os 原生菜单栏可以工作:显示在最左上角的那个。

【问题讨论】:

  • 您是否尝试为菜单分配操作?

标签: c++ macos qt qmainwindow qmenubar


【解决方案1】:

为了让 OS X 能够控制菜单栏,您需要创建没有父窗口部件的菜单栏。这意味着通常在 mainwindow.cpp 文件中,您必须在代码中创建菜单栏。

这是我的代码,它也放入程序菜单下拉菜单中的关于和首选项菜单项中,这是 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);
    // ...
}

PreferencesAbout 是处理各自窗口的类,不包括代码。

您需要删除自动生成的主窗口 ui 表单中的菜单栏。

【讨论】:

  • 同样的事情发生在 Kubuntu 20.10 上,解决方法是这样的。
【解决方案2】:

我知道它晚了 4 年,但我遇到了同样的问题并在 ubuntu/Mac OS 的 Qt 论坛上发现了这个问题。

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

在声明主窗口 w 之前,将以下内容添加到 main.cpp:

QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);

在我的例子中,我的 main.cpp 文件现在看起来像:

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();

}

【讨论】:

【解决方案3】:

获得最多支持的答案适用于 Python,但问题是适用于 C++。不过方法是一样的。

您可以在 mainwindow.cpp 中的构造函数顶部添加这一行:

menuBar()->setNativeMenuBar(false);

【讨论】:

    【解决方案4】:
    menu = self.menuBar()
    

    menu.setNativeMenuBar(False)

    【讨论】:

    • 答案可能更详尽。
    【解决方案5】:

    我在 ubuntu 中使用 python

    发布过同样的问题

    我使用了菜单栏的 setNativeMenubar 方法。您可以在 c++ pyqt 文档中找到它。

        menu = self.menuBar()
        menu.setNativeMenuBar(False)
    

    【讨论】:

    • 这解决了我的问题 - 我建议接受这个作为答案
    • 这里也一样。解决了这个问题。 @joshf 接受这个答案吗?
    【解决方案6】:

    这是 OS X 上相当老的 Qt 错误。您可以通过调用 QMenuBar::addAction、QMenuBar::removeAction 和 QMenuBar::insertAction 来使用 QMenu 和 QMenuBar。 这个技巧是通过调用 QMenu::menuAction 方法来完成的。

    检查下面的代码:

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

    你也可以检查我的另一个答案here,代码 sn-p 准备编译和运行。

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多