Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems Adding Menu to Menu Bar on Mac
Forum Updated to NodeBB v4.3 + New Features

Problems Adding Menu to Menu Bar on Mac

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 711 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jFadaie
    wrote on last edited by
    #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 work. 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&#40;&#41;;
    

    }
    @

    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.

    1 Reply Last reply
    0
    • sierdzioS Online
      sierdzioS Online
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You need to write that code after ui->setupUi(this) call. Otherwise, the UI is not ready for the changes.

      (Z(:^

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved