Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Menu not showing up in menuBar

    General and Desktop
    16
    18
    25744
    Loading More Posts
    • 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.
    • B
      babazaroni last edited by

      Hello,

      I created the default app from the Creator template Qt Widget Project/Qt Gui Application, and then tried to add a File menu to the menuBar. The File menu did not show up. What's wrong, it seems so simple?

      As you can see from the code, I tried to get the menuBar in two different ways.

      @#include <QMainWindow>

      namespace Ui {
      class MainWindow;
      }

      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

      QMenu *fileMenu;
      QAction *quitAction;
      

      private:
      Ui::MainWindow *ui;
      };
      @

      @#include "mainwindow.h"
      #include "ui_mainwindow.h"

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

      quitAction = new QAction("&Quit", this);
      

      // fileMenu = menuBar()->addMenu(tr("&File"));
      fileMenu = ui->menuBar->addMenu(tr("&File"));
      fileMenu->addAction(quitAction);
      connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
      }

      MainWindow::~MainWindow()
      {
      delete ui;
      }
      @

      ui_mainwindow.h

      @#include <QtCore/QVariant>
      #include <QtGui/QAction>
      #include <QtGui/QApplication>
      #include <QtGui/QButtonGroup>
      #include <QtGui/QHeaderView>
      #include <QtGui/QMainWindow>
      #include <QtGui/QMenuBar>
      #include <QtGui/QStatusBar>
      #include <QtGui/QToolBar>
      #include <QtGui/QWidget>

      QT_BEGIN_NAMESPACE

      class Ui_MainWindow
      {
      public:
      QWidget *centralWidget;
      QMenuBar *menuBar;
      QToolBar *mainToolBar;
      QStatusBar *statusBar;

      void setupUi(QMainWindow *MainWindow)
      {
          if (MainWindow->objectName().isEmpty())
              MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
          MainWindow->resize(400, 300);
          centralWidget = new QWidget(MainWindow);
          centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
          MainWindow->setCentralWidget(centralWidget);
          menuBar = new QMenuBar(MainWindow);
          menuBar->setObjectName(QString::fromUtf8("menuBar"));
          menuBar->setGeometry(QRect(0, 0, 400, 22));
          MainWindow->setMenuBar(menuBar);
          mainToolBar = new QToolBar(MainWindow);
          mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
          MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
          statusBar = new QStatusBar(MainWindow);
          statusBar->setObjectName(QString::fromUtf8("statusBar"));
          MainWindow->setStatusBar(statusBar);
      
          retranslateUi(MainWindow);
      
          QMetaObject::connectSlotsByName(MainWindow);
      } // setupUi
      
      void retranslateUi(QMainWindow *MainWindow)
      {
          MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
      } // retranslateUi
      

      };

      namespace Ui {
      class MainWindow: public Ui_MainWindow {};
      } // namespace Ui

      QT_END_NAMESPACE
      @

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        I just copied your code to my project and it works perfectly.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • Z
          ZapB last edited by

          Try doing a rebuild of your project. Sounds like you have a stale build.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply Reply Quote 0
          • B
            babazaroni last edited by

            Thanks guys, still no luck. I deleted the build folder and user settings and rebuilt. I guess I'll need to start with an existing demo app where the menus work and add in my app code.

            1 Reply Last reply Reply Quote 0
            • G
              giesbert last edited by

              you could zip your complete project and put it to pastebin, but otherwise, I see no chance to look for a bug, as the posted code works.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply Reply Quote 0
              • H
                HuXiKa last edited by

                Works for me too.

                If you can find faults of spelling in the text above, you can keep them.

                1 Reply Last reply Reply Quote 0
                • L
                  Loulou last edited by

                  I had the same issue of the menu bar not showing (config: Qt 5.2 , ubuntu 13.10). This trick solved the issue:
                  @menuBar()->setNativeMenuBar(false);@

                  D 1 Reply Last reply Reply Quote 0
                  • F
                    fightling last edited by

                    [quote author="Loulou" date="1387722347"]I had the same issue of the menu bar not showing (config: Qt 5.2 , ubuntu 13.10). This trick solved the issue:
                    @menuBar()->setNativeMenuBar(false);@[/quote]

                    Thank you that great & simple solution. Worked for me!

                    1 Reply Last reply Reply Quote 0
                    • M
                      maalej last edited by

                      Thanks,

                      I encountered the same problem when I created a new QMainwindow project using the native MenuBar and adding some items to it.
                      The MenuBar does not appear after running the program, and appears only when double clicking the windoow to make it full screen.
                      the code line:
                      @menuBar()->setNativeMenuBar(false);// Trick to see native MenuBar@
                      You can say it is the trick to see the native MenuBar, but for me it has no sense especially the false thing. The question still remains why it does not appear in-spite automatic generation?

                      1 Reply Last reply Reply Quote 0
                      • W
                        willy27 last edited by

                        [quote author="Loulou" date="1387722347"]I had the same issue of the menu bar not showing (config: Qt 5.2 , ubuntu 13.10). This trick solved the issue:
                        @menuBar()->setNativeMenuBar(false);@[/quote]

                        Thank you, It also worked for me on mac os !

                        Elvis T 2 Replies Last reply Reply Quote 1
                        • D
                          DavidMc @Loulou last edited by

                          Where would I add this code to get it to work? Not sure which file it has to go in. New to QT and any help would be appreciated :)

                          ? 1 Reply Last reply Reply Quote 0
                          • dheerendra
                            dheerendra Qt Champions 2022 last edited by

                            which platform you are trying ? If Ubuntu and Mac, platform do some intelligence. Just try to add few more actions like QAction("David") , QAction("Dheeru") like this. It will start showing up. Since you are default "Quit" and "Quit" is by default available. So it may not be showing up.

                            Dheerendra
                            @Community Service
                            Certified Qt Specialist
                            http://www.pthinks.com

                            1 Reply Last reply Reply Quote 4
                            • ?
                              A Former User @DavidMc last edited by

                              @DavidMc Hi, add the following to your main.cpp:

                              QCoreApplication::​setAttribute(Qt::AA_DontUseNativeMenuBar)
                              

                              Same effect, just for all your menu bars.
                              Cheers!

                              A 1 Reply Last reply Reply Quote 1
                              • Elvis
                                Elvis @willy27 last edited by

                                @willy27 It also affect my code.

                                1 Reply Last reply Reply Quote 0
                                • T
                                  tallia1 @willy27 last edited by

                                  @willy27 Your temporary fix worked quite well. It's not pretty, but it does the job!!

                                  1 Reply Last reply Reply Quote 0
                                  • A
                                    abagu @Guest last edited by

                                    I just ran into the same problem trying to follow the Qt5 tutorial from https://doc.qt.io/qt-5/qtwidgets-tutorials-notepad-example.html on MacOS Catalina (no menu bar mystery after building), which has no warnings about differences between platforms. This saved me some time before I delve into macextras

                                    1 Reply Last reply Reply Quote 0
                                    • A
                                      astroonaut last edited by astroonaut

                                      Hi guys, very new into Qt, just got this workin on high sierra/PyCharm/PyQt 5.15.3. My menubar is called "menubar" => self.menubar.setNativeMenuBar(False)
                                      ...if you try to solve it from python side. Thanks a bunch!

                                      S 1 Reply Last reply Reply Quote 0
                                      • S
                                        steelliberty @astroonaut last edited by

                                        @astroonaut Excellent .. works- Thanks

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post