View Full Version : QScrollArea in a QTabWidget page

27th February 2012, 20:49
This should be easy and I found topics similar to this but I'm still stuck, so I'll
ask in a new thread: How do I get the contents of a tab in a QTabWidget to scroll?

I create a scroll area, set a layout to it and then insert it to a tab
widget. Later, I add widgets to that layout. However, scroll bars never
appear. Instead, the widgets get partially stacked on top of each other.
What am I doing wrong? I'm trying to avoid having to create a custom
widget for each and every tab and then using scrollarea->setWidget() but
is that really the problem?

//in header for base class:
QVBoxLayout* vlayout;

//in header for this class:
QGridLayout* grid;
QTabWidget* tabs;

//in constructor for this class:
tabs = new QTabWidget(this);
grid = new QGridLayout();
grid->setAlignment(Qt::AlignLeft | Qt::AlignTop);
QScrollArea* scrollarea = new QScrollArea(this);
tabs->addTab(scrollarea, tr("Look, items!"));

//items get added some time after construction:
SomeItem* item = new SomeItem(this); //not derived from base class, does have a layout
grid ->addWidget(item);

27th February 2012, 23:22
Are you trying to achieve:

multiple tabs each with content that can be scrolled about, or
a single tab with scroll area that grows to accommodate more content added later?

28th February 2012, 21:38
I don't see the difference between those two points... I will need to have several tabs that each scroll on their own, but once that is established, all of them can receive more items to display. The entire QTabWidget, however, will not need to be viewed through a scrolling area.

I must say, the interactions between layouts, parent pointers, basic widgets and "container" widgets can get pretty complicated. Very many possible permutations in this case, for example: when to set parent to "this" and when not, what gets added to what and in which order.

28th February 2012, 23:17
I don't see the difference between those two points...
The difference is that one adds new tabs, and the other adds new content to an existing layout. You talk a bit about adding content to existing layouts but then also talk about having to make custom widgets for each tab. It was worth clarifying whether you wanted one or the other because your code only demonstrates one.

I will need to have several tabs that each scroll on their own, but once that is established, all of them can receive more items to display. The entire QTabWidget, however, will not need to be viewed through a scrolling area.
OK, you want both.

Here is a single tab example:

#include <QtGui>
#include <QDebug>

class Widget: public QFrame {
Widget(QWidget *p = 0): QFrame(p) {
setFrameStyle(QFrame::Panel | QFrame::Plain);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("A Label", this));
layout->addWidget(new QLabel("B Label", this));

class TabWidget: public QTabWidget {

QTimer *timer;
QVBoxLayout *layout;
TabWidget(QWidget *p = 0): QTabWidget(p) {
// The stuff inside the scroll area
QWidget *container = new QWidget(this);
layout = new QVBoxLayout(container);
layout->setSizeConstraint(QLayout::SetFixedSize); // <<< this is the magic

QScrollArea *scroll = new QScrollArea(this);

// Put the scrolling area in to a tab page
addTab(scroll, "One");

// Cause a new one to be added every 2 seconds for demo purposes
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(addAnother()));

public slots:
void addAnother() {
layout->addWidget(new Widget(this));


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

TabWidget tw;

return app.exec();
#include "main.moc"

The magic is the sizeConstraint on the container's layout. This causes the layout to adopt the size of the contained widget's sizeHint()s rather than resize the contained widgets. For a variation on the theme look at the Extension Example

In general, if I create a QObject or derived class on the heap I always give it a parent thereby ensuring the memory is freed when the program exits (cleanly or by crashing, not that the latter ever happens to me ;) ).

2nd March 2012, 20:15
Yep, the size constraint was the solution:

//header for class holding tab widget
QGridLayout* dataLayout;
QWidget* page;
QTabWidget* tabs;
QVBoxLayout* mainLayout;

dataLayout = new QGridLayout();
page = new QWidget(this);
QScrollArea* scrollArea = new QScrollArea(this);
dataLayout->setSizeConstraint(QLayout::SetFixedSize); //important!
tabs->addTab(scrollArea, tr("Some items"));

//test add after construction
for (int n = 0; n < 40; ++n)
SomeItem* item = new SomeItem(this);

Without setSizeConstraint(QLayout::SetFixedSize) the items will not even be displayed
Adding items in the constructor does not work (tried after every phase)
Not using a "page" QWidget and calling scrollArea->setLayout(dataLayout)
does not work. Subclassing an actual custom Page widget from QWidget, however, is not

I wonder what use does QScrollArea::setLayout() even have?

10th March 2012, 20:16
I have discovered a refinement: setSizeConstraint(QLayout::SetFixedSize) stops the page widget from expanding in any direction when the entire tab widget is expanded, but these can be used instead.

page->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
Now the page widget will use all the space in can get, yet scroll bars will appear if that space is not enough.