saurabhsood91
saurabhsood91

Reputation: 489

Reserving Space for QScrollArea

I am using a QScrollArea with a custom QWidget. The problem I am facing is that whenever the scrollbar appears, it leads to shifting of elements in the widget. So, I want to reserve some space, so when the scrollbar appears or disappears, the widget is not affected. How can I achieve this?

Upvotes: 3

Views: 261

Answers (2)

Destiny
Destiny

Reputation: 506

I encountered this problem and just solved it(might not completely).

I set a fixed width for scroll widget and QScrollArea, and set QScrollArea Horizontal SizePolicy fixed, and hide HorizontalScrollBar. Sample code

QWidget *pWidget = new QWidget(this);
pWidget->setFixedWidth(500);
pWidget->setLayout(...)

QScrollArea *pScrollArea = new QScrollArea();
// Same with widget
pScrollArea->setFixedWidth(500);
pScrollArea->setWidget(pWidget);
pScrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
// Needed.
pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

I also find a Bugreport: QTBUG-2347 QScrollArea: a new scroll policy that reserves space for the scroll bar, and it is closed. But I don't know whether the bug is fixed and what's the solution.

Upvotes: 0

claireliu
claireliu

Reputation: 11

scrollArea->setWidgetResizable(false);

Upvotes: 1

Related Questions