Qt add menuBar, menus, and sub menus to QMainWindow

23k views Asked by At

I have a hard time adding menu Bar, menus and sub menus to Qt QMainWindow programmatically.

The following code produces an error:

QWidget::setLayout: Attempting to set QLayout "" on QMainWindow "", which already has a layout

Notes : *.The main window come out without any menu or Layout (Empty!)

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QMenuBar *menu = new QMenuBar;
            QMenu *file = new QMenu();
            file->addMenu("&File");
            menu->addMenu(file);

            QSlider *s1 = new QSlider(Qt::Horizontal);
               QSlider *s2 = new QSlider(Qt::Vertical);
               QSpinBox *sb = new QSpinBox;


               QHBoxLayout *L = new QHBoxLayout;
                L->addWidget(s1);
                L->addWidget(s2);
                L->addWidget(sb);




     QMainWindow *w = new QMainWindow;
     w->setLayout(L);
     w->show();

    return a.exec();
}
5

There are 5 answers

1
eyllanesc On BEST ANSWER

add layout to central widget:

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include<QMenuBar>
#include<QStatusBar>
#include <QMainWindow>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);

    QMainWindow *w = new QMainWindow;

    QMenuBar *menu = new QMenuBar;
    QMenu *file = new QMenu();
    file->addMenu("&File");
    menu->addMenu(file);

    QWidget *centralwidget = new QWidget(w);
    w->setCentralWidget(centralwidget);

    QSlider *s1 = new QSlider(Qt::Horizontal, centralwidget);
    QSlider *s2 = new QSlider(Qt::Vertical, centralwidget);
    QSpinBox *sb = new QSpinBox;


    QHBoxLayout *L = new QHBoxLayout(centralwidget);
    L->addWidget(s1);
    L->addWidget(s2);
    L->addWidget(sb);
    w->show();

    return a.exec();
}

enter image description here

0
Vladimir Bershov On

Each QMainWindow should have a central widget:

QMainWindow *w = new QMainWindow;

QWidget* centralWidget = new QWidget;
w->setCentralWidget( centralWidget );

centralWidget->setLayout(L);
w->show();
0
Joseph Ali On

This is the final version

#include <QApplication>

#include <QApplication>
#include<QSlider>
#include<QSpinBox>
#include<QHBoxLayout>
#include<QWidget>

#include "mainwindow.h"
#include<QMenuBar>
#include<QStatusBar>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);


            QSlider *s1 = new QSlider(Qt::Horizontal);
               QSlider *s2 = new QSlider(Qt::Vertical);
               QSpinBox *sb = new QSpinBox;


     QMainWindow *w = new QMainWindow;

     QWidget *cw = new QWidget(w);

     QMenuBar *menu = new QMenuBar(cw);


     QHBoxLayout *L = new QHBoxLayout(cw);
      L->addWidget(s1);
      L->addWidget(s2);
      L->addWidget(sb);

             QMenu *file = new QMenu("&File");
             file->addMenu("Open");
              file->addMenu("new");

              QMenu *Build = new QMenu("&Build");
              Build->addAction("Rebuild this file");
               Build->addAction("Rebuild All");

             menu->addMenu(file);
             menu->addMenu(Build);

    w->setCentralWidget(cw);

     w->show();


     QObject::connect  (s1,SIGNAL(valueChanged(int) ),  sb,SLOT(setValue(int) )   );
     QObject::connect  (s1,SIGNAL(valueChanged(int) ),  s2,SLOT(setValue(int) )   );

     QObject::connect  (s2,SIGNAL(valueChanged(int) ),  sb,SLOT(setValue(int) )   );
     QObject::connect  (s2,SIGNAL(valueChanged(int) ),  s1,SLOT(setValue(int) )   );

     QObject::connect  (sb,SIGNAL(valueChanged(int) ),  s1,SLOT(setValue(int) )    );
     QObject::connect  (sb,SIGNAL(valueChanged(int) ),  s2,SLOT(setValue(int) )    );

     return a.exec();


}
1
AudioBubble On

I realize this is an ancient post , but the answers are unnecessary complicated

basic Qt Widget application

QApplication a(argc, argv);
MainWindow w;

w.menuBar()->addAction("TEST");

w.show();

plus

     addAction("TEST"); 

     has 4 overloads to actually implement the menu option
0
antoine On

To try to clearly answer the question suggested by the title (which is not a question by itself), QMainWindow does already have an empty menu bar by default besides other things like a central widget.

To access this QMenuBar and populate it with things of your choice, just call menuBar() from your QMainWindow instance. To add a submenu to a QMenuBar, use QMenuBar::addMenu.

For example :

QAction* newAct = new QAction("save");
auto fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
auto submenu = fileMenu->addMenu("Submenu");
submenu->addAction(new QAction("action1");
submenu->addAction(new QAction("action2");

For more information, look at this Qt example : https://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html and also the QMenuBar reference https://doc.qt.io/qt-5/qmenubar.html#details