Important: Please read the Qt Code of Conduct -

Qt menubar disappears when I use scroll area

  • I want to implement a vertical scroll bar. I have implemented layouts for this(see the attached layouts.jpg), and also scroll area(see snippet in below code). Yet, when I run, the window does not have menubar. What am I missing here? I tried adding the menubar and submenus through Qt designer, as well as through code, but the window does not show the menubar.

    If i use scroll area snippet, i can use the scrollbar, but don't see menubar. However If i comment out the scroll area snippet, i can see the menubar, but lose the scroll bar. I need both of them. How to solve this?

    "Your text to link here...":

    Here is the code:-
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QScrollArea>
    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    Ui::MainWindow *ui;


    #endif // MAINWINDOW_H@

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

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    QMenu *viewMenu;
    viewMenu = menuBar()->addMenu("VIEW");
    QMenu *resolutionSubMenu = viewMenu->addMenu("Resolution");
    QAction *QVGA = resolutionSubMenu->addAction("QVGA");

    QImage image(":\\image.jpg");
    ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);ui->referenceImageLabel->setMinimumSize(480, 640);
    /*Scroll area snippet*/
    QScrollArea *p_ScrollArea = new QScrollArea(this);
    p_ScrollArea->setMinimumSize(width(), height());


    delete ui;

    #include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWindow w;;

    return a.exec&#40;&#41;;


  • Lifetime Qt Champion


    Why ?


    The menu bar is already put at the right place for you when you use a QMainWindow

    QScrollArea *p_ScrollArea = new QScrollArea(this);
    p_ScrollArea->setMinimumSize(width(), height());

    You scroll area is just floating.

    Since you already use Designer to create your UI, why don't you also setup the scroll area and menus there ?

  • Hi SGaist,
    For the 1st question, when i added menubar through the designer, and run the program, the menubar was not there. so i added the line programmatically, but that did not help either.

    For the 2nd question, i do not know how to implement scroll bar for three qlabels through designer mode. If you have a tutorial where multiple qlables can be scrolled using a single vertical or horizontal scrollbar, through designer mode, and can share, i'd be most grateful.

    Also, what's a floating scroll area?

  • Lifetime Qt Champion

    The first thing you should do is have a look at Qt's example and tutorials you should then be able to create your widget.

    It's floating in the sense that you didn't set it as central widget nor in a layout thus you have to handle it's size and position by hand.

Log in to reply