Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Making menu bar overlap central widget in QMainWindow



  • I'm trying to implement an auto-hiding menu bar when my application is in full screen mode. I am using a QMainWindow with a menubar and a central widget. I have implemented this code, and it works after I setMouseTracking(true).

    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        if (isFullScreen()) {
            if (event->pos().y() < menuBar()->sizeHint().height())
                menuBar()->show();
            else
                menuBar()->hide();
        }
    }
    

    The problem is that when the menu bar appears, the central widget moves down to accommodate it and it's jarring. I would rather have the menu bar overlap the central widget so that it doesn't move. Is this possible? If so, how?


  • Lifetime Qt Champion

    Hi
    Try this in ctor
    ui->menubar->setParent( centralWidget() );
    and add
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    ui->menubar->resize(width(), ui->menubar->height());
    }

    Tested fast with full screen and seems to work.

    alt text


  • Lifetime Qt Champion

    Hi
    Try this in ctor
    ui->menubar->setParent( centralWidget() );
    and add
    void MainWindow::resizeEvent(QResizeEvent *event)
    {
    ui->menubar->resize(width(), ui->menubar->height());
    }

    Tested fast with full screen and seems to work.

    alt text



  • @mrjj Thanks, this pointed me in the right direction. Here is what I ultimately ended up doing: https://github.com/xaos-project/XaoS/blob/master/src/ui/mainwindow.cpp#L954

    Note that my centralWidget can optionally be a QGLWidget, which complicates things. The menubar won't display over it properly and I also had other problems when the QGLWidget completely filled the screen, such as popup menus not displaying, so I just disabled the autohiding menu if the compile-time USE_OPENGL flag is set.


Log in to reply