-2

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?

bur
  • 604
  • 5
  • 20
  • 1
    The only way is to ensure that your window content width is less than the total width minus the Vscrollbar width. Qt has no easy way to know if there is important content in those few pixels, such as the open or close widget of a right-aligned UI element. – Gem Taylor Oct 29 '18 at 12:45
  • Did you manage to compile this code? – scopchanov Oct 29 '18 at 21:42
  • Well, yes, but this is just a snippet. – bur Oct 30 '18 at 11:00

2 Answers2

1

You can supress the scrollbar using a scroll bar policy:

scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

Note that the contents might be clipped.

For cases where the scroll area would have more room available, you can try widgetResizable:

scroll->setWidgetResizable(true);
king_nak
  • 11,313
  • 33
  • 58
  • If I use this, the content is indeed clipped. The part of the QFrame that's hidden behind the vertical scrollbar will still be hidden. I'm not sure if that's acceptable in this case, but I'll keep it in mind if I don't find another solution. Thanks. – bur Oct 30 '18 at 17:55
  • Check my update. It might help in your case. Otherwise: there is not enough horizontal space for the content. So what can you do except clip it, or display a scrollbar? You could give it more space from the beginning, to account for the eventual vertical scrollbar... – king_nak Oct 30 '18 at 18:05
  • `setWidgetResizable` was already enabled (as seen in OP). But you comment gave me the idea to just manually set the minimum width for the dialog. I'll post an answer. – bur Oct 31 '18 at 20:13
1

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());
bur
  • 604
  • 5
  • 20