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. QMenuBar not displaying
Forum Update on Monday, May 27th 2025

QMenuBar not displaying

Scheduled Pinned Locked Moved Solved General and Desktop
qmenubarnot displaying
3 Posts 2 Posters 305 Views
  • 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.
  • S Offline
    S Offline
    Saviz
    wrote on 1 Jan 2023, 00:06 last edited by
    #1

    I am trying to create a simple QMenuBar with one QMenu.

    mainwindow.h file:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMenuBar>
    #include <QMenu>
    
    class MainWindow : public QMainWindow
    {
    // Macros
        Q_OBJECT
    
    // Constructors
    public:
        MainWindow(QWidget *parent = nullptr);
    
    // Destructors
    public:
        ~MainWindow();
    
    // QWidgets
    private:
        QWidget *QWidget_CenteralWidget;
        QMenuBar *QMenuBar_Bar;
        QMenu *QMenu_File;
    
    // Methods
    private:
        void setupGUI();
    };
    
    #endif
    

    mainwindow.cpp file:

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        setupGUI();
    }
    
    MainWindow::~MainWindow()
    {
    }
    

    mainwindow_gui.cpp file:

    #include "mainwindow.h"
    
    void MainWindow::setupGUI()
    {
        // MainWindow
        if (this->objectName().isEmpty())
        {
            this->setObjectName(QString::fromUtf8("QMainWindow_MainWindow"));
        }
    
        this->setWindowState(Qt::WindowMaximized);
    
    
        // Centeral Widget
        QWidget_CenteralWidget = new QWidget(this);
    
        QWidget_CenteralWidget->setStyleSheet("background-color: gray;");
    
        this->setCentralWidget(QWidget_CenteralWidget);
    
    
        // QMenuBar
        QMenuBar_Bar = new QMenuBar(this);
        QMenuBar_Bar->setVisible(true);
        QMenuBar_Bar->setStyleSheet("background-color: red;");
    
        QMenu_File = new QMenu(QMenuBar_Bar);
        QMenu_File->setObjectName(
    
                    QString::fromUtf8("QMenu_File")
                    );
    
    
        QMenuBar_Bar->addMenu(QMenu_File);
    
        this->setMenuBar(QMenuBar_Bar);
    }
    
    

    Here is the output:

    Image.png

    As you can see, the menu bar is nowhere to be found.

    Why is this happening?

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 1 Jan 2023, 00:43 last edited by Chris Kawa 1 Jan 2023, 00:50
      #2

      Your menu has no text, so it occupies no space. Give it something to show e.g.

      QMenu_File = new QMenu("&File", QMenuBar_Bar);
      QMenuBar_Bar->addMenu(QMenu_File);
      

      or a shorthand:

      QMenu_File = QMenuBar_Bar->addMenu("&File");
      

      Btw. setObjectName has an overload that takes QAnyStringView, so you can use it and save on dynamically allocating temporary QString: setObjectName("QMainWindow_MainWindow").

      Btw.2 Calling this->setWindowState(Qt::WindowMaximized);or any other variant of show() in the constructor is a bad practice. A widget should not dictate how it is shown. This should be the responsibility of the code that creates it. It would also make calling QMenuBar_Bar->setVisible(true); unnecessary, as it would be shown automatically when the parent is shown.

      S 1 Reply Last reply 1 Jan 2023, 03:06
      2
      • C Chris Kawa
        1 Jan 2023, 00:43

        Your menu has no text, so it occupies no space. Give it something to show e.g.

        QMenu_File = new QMenu("&File", QMenuBar_Bar);
        QMenuBar_Bar->addMenu(QMenu_File);
        

        or a shorthand:

        QMenu_File = QMenuBar_Bar->addMenu("&File");
        

        Btw. setObjectName has an overload that takes QAnyStringView, so you can use it and save on dynamically allocating temporary QString: setObjectName("QMainWindow_MainWindow").

        Btw.2 Calling this->setWindowState(Qt::WindowMaximized);or any other variant of show() in the constructor is a bad practice. A widget should not dictate how it is shown. This should be the responsibility of the code that creates it. It would also make calling QMenuBar_Bar->setVisible(true); unnecessary, as it would be shown automatically when the parent is shown.

        S Offline
        S Offline
        Saviz
        wrote on 1 Jan 2023, 03:06 last edited by
        #3

        @Chris-Kawa Thank you for your solutions and suggestions. I made sure to use the overload and to change my code to use the following in the appropriate place:

        this->setWindowState(Qt::WindowMaximized);

        Thank you again.

        1 Reply Last reply
        0

        1/3

        1 Jan 2023, 00:06

        • Login

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