Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单QMainWindow代码、Qt Creator Mac OS的菜单栏不显示_C++_Macos_Qt_Qmainwindow_Qmenubar - Fatal编程技术网

C++ 简单QMainWindow代码、Qt Creator Mac OS的菜单栏不显示

C++ 简单QMainWindow代码、Qt Creator Mac OS的菜单栏不显示,c++,macos,qt,qmainwindow,qmenubar,C++,Macos,Qt,Qmainwindow,Qmenubar,我在向Qt桌面应用程序中的内置菜单栏添加菜单项时遇到问题。我将QMainWindow类参考文档中提供的用于创建菜单的代码复制到一个非常简单的应用程序中。不幸的是,在代码运行时它没有显示。我只是想在菜单栏上添加一个“文件”菜单。我正在运行MacOSX 10.9.3和QtCreator 5.3.1 下面是我代码的截图。我在mainwindow.cpp源代码中尝试了未注释和注释的代码 main window.cpp #include "mainwindow.h" #include "ui_mainwi

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

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

main window.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;
}
#include "mainwindow.h"
#include <QApplication>

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

    return a.exec();
}
main window.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
任何帮助都将不胜感激!谢谢大家!


注意:我知道使用setNativeMenuBar(false)是可行的,但我希望mac os本机菜单栏能够工作:显示在最左上角的菜单栏。

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

您还可以用准备编译和运行的代码片段检查我的另一个答案。

我在ubuntu中用python发布了相同的答案

我使用了menubar的setNativeMenubar方法。你可以在C++ Pyqt文档中找到这个。
    menu = self.menuBar()
    menu.setNativeMenuBar(False)

StnnAutoMeNeNuBar(false)

对Python的投票最多,但问题是C++。然而,方法是相同的

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


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

在声明main窗口之前,请将以下内容添加到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();

}

为了让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);
    // ...
}
首选项
关于
是处理各自窗口的类,不包括代码


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

您是否尝试为菜单分配操作?答案可能会更详细一些。这为我解决了问题-我建议在此接受相同的答案。解决了这个问题@joshf接受这个答案吗?是的,现在是2020年,对于在MacOS上构建的用户来说,新手Qt教程仍然是第一个绊脚石。让我大吃一惊。所有平台的一个代码作为Qt的卖点?不完全是这样。同样的事情发生在Kubuntu 20.10上,解决方案是这样的。
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
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();
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);
    // ...
}