Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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...":http://i.stack.imgur.com/Nj9OG.jpg

    Here is the code:-
    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

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

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;

    };

    #endif // MAINWINDOW_H@

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

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    layout()->setMenuBar(menuBar());
    QMenu *viewMenu;
    viewMenu = menuBar()->addMenu("VIEW");
    QMenu *resolutionSubMenu = viewMenu->addMenu("Resolution");
    QAction *QVGA = resolutionSubMenu->addAction("QVGA");

    QImage image(":\\image.jpg");
    ui->referenceImageLabel->setPixmap(QPixmap::fromImage(image));
    ui->deltaImageLabel->setPixmap(QPixmap::fromImage(image));
    ui->reconstructedImageLabel->setPixmap(QPixmap::fromImage(image));
    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->setAutoFillBackground(true);
    p_ScrollArea->setMinimumSize(width(), height());
    p_ScrollArea->setWidget(ui->centralWidget);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }@

    main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>

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

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

    }@


  • Lifetime Qt Champion

    Hi,

    Why ?

    @layout()->setMenuBar(menuBar());@

    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->setAutoFillBackground(true);
    p_ScrollArea->setMinimumSize(width(), height());
    p_ScrollArea->setWidget(ui->centralWidget);
    @

    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