Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

c++ - MenuBar Not Showing for Simple QMainWindow Code, Qt Creator Mac OS

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.

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!

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had same issued in ubuntu with python

I used menubar's setNativeMenubar method. You can find this in c++ pyqt docs.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...