Search code examples
qtqmainwindowqmenuqmenubar

Put QMenuBar at QMainWindow Bottom in QT


Is there any way to put a QMenuBar at screen bottom (I mean, at QMainWindow bottom)?

I'm working on my thesis project, and my director asked me to put a QMenuBar at screen bottom. Is this possible?, I have been trying adjusting the menubar geometry. In Qt Designer I can move the bar position, but when I run my project, the menu bar is always up.

Thanks in advance.


Solution

  • Don't use the default QMenuBar provided with the QMainWindow. Instead create your own. This proof of concept example creates a new QMenuBar which is added to a QVBoxLayout which was added to the mainwindow:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QMenuBar* bar = new QMenuBar(this);
        ui->verticalLayout->addWidget(bar);
    
        QMenu* menu1 = new QMenu("First menu", bar);
        menu1->addMenu("Foo");
        menu1->addMenu("Bar");
    
        QMenu* menu2 = new QMenu("Second menu", bar);
        menu2->addMenu("Foo");
        menu2->addMenu("Bar");
    
        bar->addMenu(menu1);
        bar->addMenu(menu2);
    }
    

    This works at least in Windows.