I have a simple dialog with a QScrollArea inside it:
// Vertical container for the dialog
QVBoxLayout *cont = new QVBoxLayout;
this->setLayout(cont); //"this" is my derived QDialog class
// ScrollArea for iconFrame
QScrollArea *scroll = new QScrollArea;
cont->insertWidget(0, scroll );
// The frame to be added to the QScrollArea
QFrame *iconFrame = new QFrame;
scroll->setWidget(iconFrame);
scroll->setWidgetResizable(true);
// Grid layout for iconFrame
QGridLayout *grid = new QGridLayout;
iconFrame->setLayout(grid);
// Second child widget for the dialog
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
cont->insertWidget(1, buttonBox);
int maxcol = int(ceil(sqrt(numberOfButtons)));
if(maxcol > 6) maxcol = 6;
for(int i=0; i<numberOfButtons; i++)
{
QPushButton *button= new QPushButton("My Button");
button->setFixedSize(48, 48);
int row = int(floor(i/maxcol));
grid->addWidget(button, row, i-row*maxcol);
}
Since there is a maximum of 6 columns, the frame and the dialog grow vertically.
It works as expected, except that the horizontal scrollbar is being drawn only because of the added width from the vertical scrollbar.
I've tried different combinations of sizePolicies and sizeConstraints, but nothing seems to have any effect.
How do I get rid of the horizontal scrollbar?
Not so much a solution as it is a workaround, but it works.
iconFrame->adjustSize(); // Only necessary when contents of iconFrame have changed
if(iconFrame->height() > scroll->height())
scroll->setMinimumWidth(iconFrame->width()+scroll->verticalScrollBar()->height());