View Full Version : QScrollArea confusion

25th January 2014, 15:04
Hi I'm trying to use a QScrollArea to manage the size of a certain widget.

This widget is a list of smaller widgets (using a QVBoxLayout) which can be dynamically resized and which starts empty, and hence invisible.

I can fully see and use the widget if I set it as the central widget of a QMainWindow. this means I can add widgets to the list, delete them etc.. and I can see everything.

Then I decided I'd like some scroll bars to manage the size of the window. So I figured I could just create a QScrollArea, set the widget as the QScrollArea widget, and then set the QScrollArea as the central widget of the main window. I didn't do any configuation of size policies or anything thinking that I'd at least see something (anything!) first. The result is that I can't see anything. The widget is simply not visible.

I have been reading the documention on size policies and what not on the QScrollArea page, but I have totally failed to understand this. I just can't understand why not setting some of these things have caused my widget to become totally invisible even when I add widgets to the list.

Again, when I set this widget as the central widget of a QMainWindow, everything works as expected. The pseudo code is:

mainWindow the_window;
listWidget the_list;

The above gives a working widget, without scroll bars

mainWindow the_window;
listWidget the_list;
QScrollArea* the_area = new QScrollArea();


The above gives a window with nothing in it, no matter how many widgets I add to the list.

If it's helpful, I'm using Qt 4.8.4 on ubuntu 13.10.

25th January 2014, 16:40
#include <QtGui>

class Container : public QWidget

explicit Container(QWidget *parent = 0) : QWidget(parent)
m_layout = new QVBoxLayout(this);

connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout()));

QTimer m_timer;
QVBoxLayout *m_layout;

private slots:
void onTimeout()
static int counter = 0;
qDebug() << Q_FUNC_INFO << counter;

QLabel *label = new QLabel(QString::number(counter++));

int main(int argc, char** argv)
QApplication app(argc, argv);

QScrollArea scrollArea;

scrollArea.setWidget(new Container(&scrollArea));


return app.exec();

#include "scrollareatest.moc"


25th January 2014, 21:43
Thanks a lot for responding, The example was helpful, in that it encouraged me to try the 'adjustSize()' function.

This finally caused the widgets within my list to be displayed.

Basically after I add a widget to the list of widgets, I need to call the adjustSize() function to get the list widget to resize itself and become visible.

However I'm still confused why the default behavior while set as the central widget for a QMainWindow didn't require me to call adjustSize(). It seems odd to me that the two should behave differently.

26th January 2014, 11:35
In the case of being a QMainWindow central widget, the container is part of a layout, the QMainWindow layout. In the QScrollArea case it is not and it is neither a top level window (which would resize itself).