PDA

View Full Version : Scrollbar covers Labels in ScrollArea



mikrocat
29th January 2016, 10:30
Hi,

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);
ui->scrollAreaWidgetContents->setLayout(verticalLayoutForWidget);

QGroupBox *TypeGroupBox[NumberOfAvailableTypes];

for (int i = 0; i < NumberOfAvailableTypes; i++){
TypeGroupBox[i] = new QGroupBox(ui->AvailableDataScrollArea);
TypeGroupBox[i]->setSizePolicy(QSizePolicy::Preferred,QSizePolicy:: Fixed);
TypeGroupBox[i]->setTitle(AllData.at(i)->NameWithoutNumber());
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]);
DataCheckbox[j]->setText(AllData.at(i)->NameWithNumber());

horizontalLayout[j] = new QHBoxLayout();
horizontalLayout[j]->addWidget(DataCheckbox[j]);
verticalLayout[i]->addLayout(horizontalLayout[j]);
}
}
verticalLayoutForWidget->addWidget(TypeGroupBox[i]);
}
verticalLayoutForWidget->addSpacerItem(new QSpacerItem(40,20,QSizePolicy::Minimum, QSizePolicy::Expanding));

ChrisW67
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.

mikrocat
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.

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

ChrisW67
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.