View Full Version : Scrollbar covers Labels in ScrollArea

29th January 2016, 10:30

I have a ScrollArea with many groupboxes which contain Checkboxes. But it seems like the size of the ScrollArea does not adapt to the layout.

QVBoxLayout* verticalLayout[NumberOfAvailableTypes];
QVBoxLayout* verticalLayoutForWidget = new QVBoxLayout(ui->AvailableDataScrollArea);

QGroupBox *TypeGroupBox[NumberOfAvailableTypes];

for (int i = 0; i < NumberOfAvailableTypes; i++){
TypeGroupBox[i] = new QGroupBox(ui->AvailableDataScrollArea);
TypeGroupBox[i]->setSizePolicy(QSizePolicy::Preferred,QSizePolicy:: Fixed);
verticalLayout[i] = new QVBoxLayout(TypeGroupBox[i]);

QCheckBox *DataCheckbox[AllData.at(i)->quantity()];
QHBoxLayout* horizontalLayout[AllData.at(i)->quantity()];

for (int j = 1; j <= AllData.at(i)->quantity(); j++){
DataCheckbox[j] = new QCheckBox(TypeGroupBox[i]);

horizontalLayout[j] = new QHBoxLayout();
verticalLayoutForWidget->addSpacerItem(new QSpacerItem(40,20,QSizePolicy::Minimum, QSizePolicy::Expanding));

29th January 2016, 21:02
The scroll bar adapts to the size of the content widget (scrollAreaWidgetContents I assume) but that size is not changing here. Packing widget into its layout will, in general, not change the containers size. Try setting the vertical size policy of scrollAreaWidgetContents to Fixed so it is forced to match the aggregate of the contents vertical size hints.

1st February 2016, 07:03
SizePolicy of scrollAreaWidgetContents is horizontal and vertical: fixed now

SizePolicy of ScrollArea is horizontal: preferred; vertical: expanding

sizeAdjustPolicy: AdjustToContens

When I set the horizontal ScrollBar ScrollBarAsNeeded it seems to work. But I dont want to have a horizontal ScrollBar. I want the ScrollArea to expand horizontal when it's layout changes.

11th February 2016, 07:26
It still does not work. :(

11th February 2016, 20:48
You want the QScrollArea to abandon its reason for existing and grow to accomodate its content. That is precisely what it is designed not to do. The scroll area gets the space allocated to it by its parent layout/widget and allows you to display an arbitrary sized widget within that space, using scrollbars as needed.

To defeat the default behaviour you have to do the work. When the contentsWidget() changes size you must manually change (force) the horizontal dimension of the QScrollArea to accomodate the contained widget's width plus a margin and width for a vertical scroll bar if one is present.