Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QScrollArea Class Reference

The QScrollArea class provides a scrolling view onto another widget. More...

#include <qscrollarea.h>

+ Inheritance diagram for QScrollArea:
+ Collaboration diagram for QScrollArea:

Public Member Functions

 QScrollArea (QWidget *parent=nullptr)
 Constructs an empty scroll area with the given parent.
 
 ~QScrollArea ()
 Destroys the scroll area and its child widget.
 
QWidgetwidget () const
 Returns the scroll area's widget, or \nullptr if there is none.
 
void setWidget (QWidget *widget)
 Sets the scroll area's widget.
 
QWidgettakeWidget ()
 Removes the scroll area's widget, and passes ownership of the widget to the caller.
 
bool widgetResizable () const
 
void setWidgetResizable (bool resizable)
 
QSize sizeHint () const override
 \reimp
 
bool focusNextPrevChild (bool next) override
 \reimp
 
Qt::Alignment alignment () const
 
void setAlignment (Qt::Alignment)
 
void ensureVisible (int x, int y, int xmargin=50, int ymargin=50)
 Scrolls the contents of the scroll area so that the point (x, y) is visible inside the region of the viewport with margins specified in pixels by xmargin and ymargin.
 
void ensureWidgetVisible (QWidget *childWidget, int xmargin=50, int ymargin=50)
 

Protected Member Functions

 QScrollArea (QScrollAreaPrivate &dd, QWidget *parent=nullptr)
 
bool event (QEvent *) override
 \reimp
 
bool eventFilter (QObject *, QEvent *) override
 \reimp
 
void resizeEvent (QResizeEvent *) override
 \reimp
 
void scrollContentsBy (int dx, int dy) override
 \reimp
 
QSize viewportSizeHint () const override
 \reimp
 

Properties

bool widgetResizable
 whether the scroll area should resize the view widget
 
Qt::Alignment alignment
 the alignment of the scroll area's widget
 

Detailed Description

The QScrollArea class provides a scrolling view onto another widget.

\inmodule QtWidgets

A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with setWidget(). For example:

QImage image("happyguy.png");
scrollArea->setBackgroundRole(QPalette::Dark);
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition qpixmap.cpp:1433
The QScrollArea class provides a scrolling view onto another widget.
Definition qscrollarea.h:17
void setWidget(QWidget *widget)
Sets the scroll area's widget.

The code above creates a scroll area (shown in the images below) containing an image label. When scaling the image, the scroll area can provide the necessary scroll bars:

\table \row

  • \inlineimage qscrollarea-noscrollbars.png
  • \inlineimage qscrollarea-onescrollbar.png
  • \inlineimage qscrollarea-twoscrollbars.png \endtable

The scroll bars appearance depends on the currently set \l {Qt::ScrollBarPolicy}{scroll bar policies}. You can control the appearance of the scroll bars using the inherited functionality from QAbstractScrollArea.

For example, you can set the QAbstractScrollArea::horizontalScrollBarPolicy and QAbstractScrollArea::verticalScrollBarPolicy properties. Or if you want the scroll bars to adjust dynamically when the contents of the scroll area changes, you can use the \l {QAbstractScrollArea::horizontalScrollBar()}{horizontalScrollBar()} and \l {QAbstractScrollArea::verticalScrollBar()}{verticalScrollBar()} functions (which enable you to access the scroll bars) and set the scroll bars' values whenever the scroll area's contents change, using the QScrollBar::setValue() function.

You can retrieve the child widget using the widget() function. The view can be made to be resizable with the setWidgetResizable() function. The alignment of the widget can be specified with setAlignment().

Two convenience functions ensureVisible() and ensureWidgetVisible() ensure a certain region of the contents is visible inside the viewport, by scrolling the contents if necessary.

Definition at line 16 of file qscrollarea.h.

Constructor & Destructor Documentation

◆ QScrollArea() [1/2]

QScrollArea::QScrollArea ( QWidget * parent = nullptr)
explicit

Constructs an empty scroll area with the given parent.

See also
setWidget()

Definition at line 103 of file qscrollarea.cpp.

◆ ~QScrollArea()

QScrollArea::~QScrollArea ( )

Destroys the scroll area and its child widget.

See also
setWidget()

Definition at line 131 of file qscrollarea.cpp.

◆ QScrollArea() [2/2]

QScrollArea::QScrollArea ( QScrollAreaPrivate & dd,
QWidget * parent = nullptr )
protected

Definition at line 116 of file qscrollarea.cpp.

Member Function Documentation

◆ alignment()

Qt::Alignment QScrollArea::alignment ( ) const

Definition at line 508 of file qscrollarea.cpp.

◆ ensureVisible()

void QScrollArea::ensureVisible ( int x,
int y,
int xmargin = 50,
int ymargin = 50 )

Scrolls the contents of the scroll area so that the point (x, y) is visible inside the region of the viewport with margins specified in pixels by xmargin and ymargin.

If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.

Definition at line 416 of file qscrollarea.cpp.

◆ ensureWidgetVisible()

void QScrollArea::ensureWidgetVisible ( QWidget * childWidget,
int xmargin = 50,
int ymargin = 50 )
Since
4.2

Scrolls the contents of the scroll area so that the childWidget of QScrollArea::widget() is visible inside the viewport with margins specified in pixels by xmargin and ymargin. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.

Definition at line 446 of file qscrollarea.cpp.

◆ event()

bool QScrollArea::event ( QEvent * e)
overrideprotected

\reimp

Definition at line 273 of file qscrollarea.cpp.

◆ eventFilter()

bool QScrollArea::eventFilter ( QObject * o,
QEvent * e )
overrideprotected

\reimp

Definition at line 294 of file qscrollarea.cpp.

◆ focusNextPrevChild()

bool QScrollArea::focusNextPrevChild ( bool next)
override

\reimp

Definition at line 400 of file qscrollarea.cpp.

◆ resizeEvent()

void QScrollArea::resizeEvent ( QResizeEvent * )
overrideprotected

\reimp

Definition at line 313 of file qscrollarea.cpp.

◆ scrollContentsBy()

void QScrollArea::scrollContentsBy ( int dx,
int dy )
overrideprotected

\reimp

Definition at line 323 of file qscrollarea.cpp.

◆ setAlignment()

void QScrollArea::setAlignment ( Qt::Alignment alignment)

Definition at line 500 of file qscrollarea.cpp.

◆ setWidget()

void QScrollArea::setWidget ( QWidget * widget)

Sets the scroll area's widget.

The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.

The widget's \l{QWidget::setAutoFillBackground()}{autoFillBackground} property will be set to {true}.

If the scroll area is visible when the widget is added, you must \l{QWidget::}{show()} it explicitly.

Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you \l{QWidget::}{show()} the scroll area. In this case, you can also not \l{QWidget::}{show()} the widget later.

See also
widget()

Definition at line 231 of file qscrollarea.cpp.

◆ setWidgetResizable()

void QScrollArea::setWidgetResizable ( bool resizable)

Definition at line 352 of file qscrollarea.cpp.

◆ sizeHint()

QSize QScrollArea::sizeHint ( ) const
override

\reimp

Definition at line 363 of file qscrollarea.cpp.

◆ takeWidget()

QWidget * QScrollArea::takeWidget ( )

Removes the scroll area's widget, and passes ownership of the widget to the caller.

See also
widget()

Definition at line 260 of file qscrollarea.cpp.

◆ viewportSizeHint()

QSize QScrollArea::viewportSizeHint ( ) const
overrideprotected

\reimp

Definition at line 386 of file qscrollarea.cpp.

◆ widget()

QWidget * QScrollArea::widget ( ) const

Returns the scroll area's widget, or \nullptr if there is none.

See also
setWidget()

Definition at line 202 of file qscrollarea.cpp.

◆ widgetResizable()

bool QScrollArea::widgetResizable ( ) const

Definition at line 346 of file qscrollarea.cpp.

Property Documentation

◆ alignment

Qt::Alignment QScrollArea::alignment
readwrite

the alignment of the scroll area's widget

Since
4.2

A valid alignment is a combination of the following flags: \list

Definition at line 20 of file qscrollarea.h.

◆ widgetResizable

bool QScrollArea::widgetResizable
readwrite

whether the scroll area should resize the view widget

If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget()->resize(), and the scroll area will automatically adjust itself to the new size.

If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

Definition at line 19 of file qscrollarea.h.


The documentation for this class was generated from the following files: