PDA

View Full Version : Scrollbar in scroll area not working



ada10
27th August 2010, 11:06
I have a mainwindow of size 480 x 350. The central widget of the main window is a scroll area of dimension 190 x 220. The code for this is as below -



scrollArea = new QScrollArea(centralWidget);
scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
scrollArea->setGeometry(QRect(210, 20, 191, 221));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new QWidget();

scrollAreaWidgetContents->setObjectName(
QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 187, 217));
scrollArea->setWidget(scrollAreaWidgetContents);
MainWindow->setCentralWidget(centralWidget);


I have a QLabel which is a child of the scrollarea. The code for this is as shown -



QLabel* tmp = new QLabel(scrollAreaWidgetContents);
tmp->setText("Custom Label");
tmp->setGeometry(QRect( 300,300,60,60));

scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );

So in the application, the scrollarea must show the scroll bars & user must be able to scroll to the qlabel , which will not be in the displayable area.

But when I run it, the scrollarea does not show the scrollbars & hence its not scrollable. How do i solve this issue? Am I doing something wrong here ?

Urthas
28th August 2010, 01:09
The central widget of the main window is a scroll area

False, at least according to the code shown. The scroll area is in fact a child of the central widget.

Also, setGeometry() does its work relative to the parent widget, and you are setting the scrollAreaContents' geometry *before* it has a parent, for example.

All in all, I think you need to very carefully re-evaluate the parent-child hierarchy of your widgets and how it is manipulated in your code. I'm not sure if this makes a difference here, but it may be a good place to start, and certainly couldn't hurt.

ada10
30th August 2010, 12:39
I have changed my mainwindow constructor as show -



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QScrollArea *scrollArea;
QWidget *scrollAreaWidgetContents;
scrollArea = new QScrollArea(this);
scrollArea->setObjectName(QString::fromUtf8("scrollArea"));
scrollArea->setGeometry(QRect(210, 20, 191, 221));
scrollArea->setWidgetResizable(true);
scrollAreaWidgetContents = new QWidget();
scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
scrollAreaWidgetContents->setGeometry(QRect(0, 0, 187, 217));
scrollArea->setWidget(scrollAreaWidgetContents);
this->setCentralWidget( scrollArea );

QLabel* tmp = new QLabel(scrollArea);
tmp->setText("label");
tmp->setGeometry(QRect(50,50,2000,2000));
tmp->show();

scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded );
}

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


Here ui is a designer form created using QtDesigner. The ui file is attached.

5120

When i run this application, the label is not shown nor are the scrollbars visible. How should I proceed ?