PDA

View Full Version : Qt menubar disappears when I use scroll area



Sai Kamat
20th May 2014, 16:21
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?
10373

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();
}

high_flyer
21st May 2014, 10:16
You are placing your central widget in to the scorll area, instead parenting to it.


QScrollArea *p_ScrollArea = new QScrollArea(centralWidget());
p_ScrollArea->setAutoFillBackground(false);
centralWidget()->setGeometry(0,0,500,500);
p_ScrollArea->setGeometry(0,0,100,100);
QLabel* pLabel = new QLabel("test");
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.