我在向Qt桌面应用程序的内置菜单栏添加菜单项时遇到了问题。我复制了QMainWindow类参考文档中提供的代码,用于创建一个非常简单的应用程序的菜单。不幸的是,它在代码运行时没有显示出来。我只是想在菜单栏中添加“文件”菜单。我正在运行Mac OSX 10.9.3和Qt Creator 5.3.1。
我的代码截图如下。我在mainwindow.cpp源代码中尝试了未注释和注释的代码。
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
任何帮助都会非常感激!谢谢!
注意:我知道使用setNativeMenuBar(false)有效,但我希望mac os本机菜单栏能够工作:显示在最左上角的那个。
答案 0 :(得分:10)
我在ubuntu中发布了与 python
相同的内容我使用了菜单栏的setNativeMenubar方法。你可以在c ++ pyqt docs中找到它。
menu = self.menuBar()
menu.setNativeMenuBar(False)
答案 1 :(得分:3)
这是OS X上相当古老的Qt bug。您可以通过调用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。
答案 2 :(得分:3)
我知道它已经晚了4年了,但是我遇到了同样的问题&amp;在Qt论坛上发现了ubuntu / Mac OS。
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 :(得分:2)
具有最多upvote的答案适用于Python,但问题是C ++。但是,方法是一样的。
您可以在 mainwindow.cpp 中的构造函数顶部添加此行:
menuBar()->setNativeMenuBar(false);
答案 4 :(得分:0)
为了使OS X能够控制菜单栏,您需要创建菜单栏没有父窗口小部件。这意味着通常在mainwindow.cpp
文件中,您必须用代码创建菜单栏。
这是我的代码,也按照Mac上的标准放置在PROGRAM菜单的About和Preferences菜单项中。
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
和About
是处理各自窗口的类,不包含代码。
您将需要删除自动生成的mainwindow ui表单中的菜单栏。
答案 5 :(得分:-2)
menu = self.menuBar()
menu.setNativeMenuBar(假)