QScrollArea Class
The QScrollArea class provides a scrolling view of another widget. More…
| Header | #include <QScrollArea> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QAbstractScrollArea |
List of all members, including inherited members
Public Functions
| QScrollArea(QWidget *parent = nullptr) | |
| ~QScrollArea() | |
| QWidget * | widget() const |
| void | setWidget(QWidget *widget) |
| QWidget * | takeWidget() |
| bool | widgetResizable() const |
| void | setWidgetResizable(bool resizable) |
| Qt::Alignment | alignment() const |
| void | setAlignment(Qt::Alignment alignment) |
Public Slots
| void | ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50) |
| void | ensureWidgetVisible(QWidget *child, int xmargin = 50, int ymargin = 50) |
Reimplemented Protected Functions
| void | resizeEvent(QResizeEvent *event) |
| void | scrollContentsBy(int dx, int dy) |
| QSize | viewportSizeHint() const |
Detailed Description
A scroll area shows part of one widget, and scroll bars move the part. Give it the widget with setWidget(); the widget keeps its own size, and the scroll bars appear when it is larger than the viewport:
QWidget *page = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(page);
for (int i = 0; i < 40; ++i)
layout->addWidget(new QLabel(QString("Line %1").arg(i)));
QScrollArea *area = new QScrollArea(window);
area->setWidget(page);
area->setWidgetResizable(true);
With setWidgetResizable(), the widget is resized to the viewport when the viewport is larger than the widget needs to be, so a layout in it fills the area, and it gets no larger than its layout's minimum size requires. The scroll area follows the widget when it is resized or its size hint changes.
A scroll area with a page of widgets: the frame, the page, and the vertical scroll bar.

See also QAbstractScrollArea and QScrollBar.
Differences from Qt
There are no height-for-width layouts in the widget. The scroll area takes over the widget: it becomes a child of the viewport, and is deleted with the scroll area unless takeWidget() is used. Scroll bars move by pixels and a single step is 20 pixels.
Member Function Documentation
QScrollArea::QScrollArea(QWidget *parent = nullptr)
Constructs a scroll area with no widget and the given parent.
QScrollArea::~QScrollArea()
Destroys the scroll area, and the widget in it.
Qt::Alignment QScrollArea::alignment() const
Returns where the widget is in the viewport when it is smaller than it. The default is Qt::AlignLeft | Qt::AlignTop.
See also setAlignment().
void QScrollArea::ensureVisible(int x, int y, int xmargin = 50, int ymargin = 50)
Scrolls the widget so that the point (x, y) in it is in view, with xmargin and ymargin pixels of the widget around it if it fits.
See also ensureWidgetVisible().
void QScrollArea::ensureWidgetVisible(QWidget *child, int xmargin = 50, int ymargin = 50)
Scrolls the widget so that child, which is in it, is in view, with xmargin and ymargin pixels around its center. Does nothing if child is not in the widget.
See also ensureVisible().
void QScrollArea::resizeEvent(QResizeEvent *event)
Reimplements QAbstractScrollArea::resizeEvent().
void QScrollArea::scrollContentsBy(int dx, int dy)
Reimplements QAbstractScrollArea::scrollContentsBy().
void QScrollArea::setAlignment(Qt::Alignment alignment)
Sets where the widget is in the viewport when it is smaller than it to alignment, for example Qt::AlignCenter.
See also alignment().
void QScrollArea::setWidget(QWidget *widget)
Makes widget the widget that the area shows, and the area its owner: the widget becomes a child of the viewport. A widget that was in the area is deleted. A widget that was never given a size is sized to its size hint.
See also widget() and takeWidget().
void QScrollArea::setWidgetResizable(bool resizable)
Sets whether the widget is resized to the viewport to resizable.
See also widgetResizable().
QWidget * QScrollArea::takeWidget()
Removes the widget from the area and returns it, and the area no longer owns it. Its parent is null.
See also setWidget().
QSize QScrollArea::viewportSizeHint() const
Reimplements QAbstractScrollArea::viewportSizeHint().
QWidget * QScrollArea::widget() const
Returns the widget that the area shows, or nullptr if there is none.
See also setWidget().
bool QScrollArea::widgetResizable() const
Returns whether the widget is resized with the area. The default is false.
See also setWidgetResizable().