我在向 Qt 桌面应用程序的内置菜单栏中添加菜单项时遇到问题。我将 QMainWindow 类引用文档中提供的用于创建菜单的代码复制到一个非常简单的应用程序中。不幸的是,它在代码运行时没有出现。我只是想在菜单栏中添加一个"file"菜单。我正在运行 Mac OSX 10.9.3 和 Qt Creator 5.3.1。
我的代码截图如下。我在 mainwindow.cpp 源代码中尝试了未注释和已注释的代码。
主窗口.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;
}
主窗口.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 native 菜单栏有效:显示在最左上角的菜单栏。
最佳答案
我在 ubuntu 中用 python 发布了同样的问题
我使用了菜单栏的 setNativeMenubar 方法。您可以在 C++ pyqt 文档中找到它。
menu = self.menuBar()
menu.setNativeMenuBar(False)
关于c++ - 简单 QMainWindow 代码的菜单栏不显示,Qt Creator Mac OS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25261760/