Ok, I see the problem. A scroll area does not resize it's widget by default. Therefore a wrapper widget (containing the flow layout) inside the scroll area can have as much space as it wants and the labels don't get laid like you want. You can make the QScrollArea to resize it's widget by setting QScrollArea::widgetResizable property as true. This is not enough for a proper solution, though. This way the labels would get laid correctly, but no scroll bars appear before there is less available space than a one single label requires. This is because FlowLayout::sizeHint() returns only the size of the biggest label. So clearly the FlowLayout needs some patching..
At first, you need a wrapper widget as we were discussing earlier. You could set the scroll area up more or less like this:
FlowLayout* flow = new FlowLayout(wrapper);
for (int i = 0; i < 20; ++i)
{
label->setFixedSize(100, 100);
flow->addWidget(label);
}
wrapper->setLayout(flow);
scroll->setWidget(wrapper);
scroll->setWidgetResizable(true);
QScrollArea* scroll = new QScrollArea(this);
QWidget* wrapper = new QWidget(scroll);
FlowLayout* flow = new FlowLayout(wrapper);
for (int i = 0; i < 20; ++i)
{
QLabel* label = new QLabel("image here", wrapper);
label->setFixedSize(100, 100);
flow->addWidget(label);
}
wrapper->setLayout(flow);
scroll->setWidget(wrapper);
scroll->setWidgetResizable(true);
To copy to clipboard, switch view to plain text mode
Then, you'll need to modify the FlowLayout to get the desirable behaviour. Make FlowLayout::minimumSize() look like this:
QSize FlowLayout
::minimumSize() const {
int w = geometry().width();
int h
= doLayout
(QRect(0,
0, w,
0),
true);
}
QSize FlowLayout::minimumSize() const
{
int w = geometry().width();
int h = doLayout(QRect(0, 0, w, 0), true);
return QSize(w, h);
}
To copy to clipboard, switch view to plain text mode
Bookmarks