对于简单的QMainWindow代码,Qt Creator Mac OS的菜单栏不显示 [英] MenuBar Not Showing for Simple QMainWindow Code, Qt Creator Mac OS
问题描述
我在将菜单项添加到Qt桌面应用程序的内置菜单栏中时遇到了问题.我将QMainWindow类参考文档中提供的用于创建菜单的代码复制到一个非常简单的应用程序中.不幸的是,代码在运行时没有显示.我只是想在菜单栏中添加文件"菜单.我正在运行Mac OSX 10.9.3和Qt Creator 5.3.1.
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 documentation for creating a menu to a very simple application. Unfortunately, it did not show up when the code was run. I am simply trying to add a "File" menu to the menu bar. I am running Mac OSX 10.9.3 and Qt Creator 5.3.1.
下面是我的代码的屏幕截图.我在mainwindow.cpp源代码中尝试了未注释和注释的代码.
The screenshots of my code are below. I tried both the uncommented and commented code in the mainwindow.cpp source.
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
任何帮助将不胜感激!谢谢!
Any help would be really appreciated! Thank you!
注意:我知道使用setNativeMenuBar(false)可以工作,但是我希望macOS本机菜单栏可以工作:显示在最左上角的菜单栏.
Note: I know using setNativeMenuBar(false) works, but I would like the mac os native menu bar to work: the one that is displayed in the top-most left corner.
推荐答案
我在ubuntu中使用 python
I had same issued in ubuntu with python
我使用了菜单栏的setNativeMenubar方法.您可以在c ++ pyqt文档中找到它.
I used menubar's setNativeMenubar method. You can find this in c++ pyqt docs.
menu = self.menuBar()
menu.setNativeMenuBar(False)
这篇关于对于简单的QMainWindow代码,Qt Creator Mac OS的菜单栏不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!