1 Attachment(s)
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?
Attachment 10373
Here is the code:- mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QScrollArea>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
layout()->setMenuBar(menuBar());
viewMenu = menuBar()->addMenu("VIEW");
QMenu *resolutionSubMenu
= viewMenu
->addMenu
("Resolution");
QAction *QVGA
= resolutionSubMenu
->addAction
("QVGA");
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*/
p_ScrollArea->setAutoFillBackground(true);
p_ScrollArea->setMinimumSize(width(), height());
p_ScrollArea->setWidget(ui->centralWidget);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
Re: Qt menubar disappears when I use scroll area
You are placing your central widget in to the scorll area, instead parenting to it.
Code:
p_ScrollArea->setAutoFillBackground(false);
centralWidget()->setGeometry(0,0,500,500);
p_ScrollArea->setGeometry(0,0,100,100);
pLabel->setGeometry(0,0,1000,1000);
p_ScrollArea->setWidget(pLabel);
Also, setting the minimum size will not do when you change the size of your window.
You need to make sure your scoll areay adjust to the windows size.