QScrollArea 类

QScrollArea 类为其它 Widget 提供卷动视图。 更多...

头: #include <QScrollArea>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAbstractScrollArea

特性

公共函数

  QScrollArea (QWidget * parent = nullptr)
virtual ~QScrollArea ()
Qt::Alignment alignment () const
void ensureVisible (int x , int y , int xmargin = 50, int ymargin = 50)
void ensureWidgetVisible (QWidget * childWidget , int xmargin = 50, int ymargin = 50)
void setAlignment (Qt::Alignment)
void setWidget (QWidget * widget )
void setWidgetResizable (bool resizable )
QWidget * takeWidget ()
QWidget * widget () const
bool widgetResizable () const

重实现公共函数

virtual bool focusNextPrevChild (bool next ) override
virtual QSize sizeHint () const override

重实现保护函数

virtual bool event (QEvent * e ) override
virtual bool eventFilter (QObject * o , QEvent * e ) override
virtual void resizeEvent (QResizeEvent *) override
virtual void scrollContentsBy (int dx , int dy ) override
virtual QSize viewportSizeHint () const override

详细描述

卷动区域用于在框架内显示子级 Widget 的内容。若 Widget 超过框架大小,视图可以提供滚动条,以便可以查看子级小部件的整个区域。指定子级 Widget 必须采用 setWidget ()。例如:

QLabel *imageLabel = new QLabel;
QImage image("happyguy.png");
imageLabel->setPixmap(QPixmap::fromImage(image));
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
					

以上代码创建包含图像标签的卷动区域 (在以下图像中展示)。当比例缩放图像时,卷动区域可以提供必要的滚动条:

滚动条外观从属目前设置的 滚动条策略 。可以控制滚动条外观使用继承功能从 QAbstractScrollArea .

例如,可以设置 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 horizontalScrollBar() and 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 () 函数。

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 () 和 ensureWidgetVisible () ensure a certain region of the contents is visible inside the viewport, by scrolling the contents if necessary.

大小提示和布局

When using a scroll area to display the contents of a custom widget, it is important to ensure that the size hint of the child widget is set to a suitable value. If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize () to ensure that the contents of the widget are shown correctly within the scroll area.

If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout's size constraint property to one which provides constraints on the minimum and/or maximum size of the layout (e.g., QLayout::SetMinAndMaxSize ) will cause the size of the scroll area to be updated whenever the contents of the layout changes.

For a complete example using the QScrollArea class, see the 图像查看器 example. The example shows how to combine QLabel and QScrollArea to display an image.

另请参阅 QAbstractScrollArea , QScrollBar ,和 图像查看器范例 .

特性文档编制

alignment : Qt::Alignment

This property holds the alignment of the scroll area's widget

A valid alignment is a combination of the following flags:

  • Qt::AlignLeft
  • Qt::AlignHCenter
  • Qt::AlignRight
  • Qt::AlignTop
  • Qt::AlignVCenter
  • Qt::AlignBottom

By default, the widget stays rooted to the top-left corner of the scroll area.

访问函数:

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

widgetResizable : bool

This property holds 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.

访问函数:

bool widgetResizable () const
void setWidgetResizable (bool resizable )

成员函数文档编制

QScrollArea:: QScrollArea ( QWidget * parent = nullptr)

构造空的卷动区域采用给定 parent .

另请参阅 setWidget ().

[虚拟] QScrollArea:: ~QScrollArea ()

销毁卷动区域及其子级 Widget。

另请参阅 setWidget ().

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 。若无法到达指定点,内容卷动到最近有效位置。2 边距的默认值为 50 像素。

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

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 。若无法到达指定点,内容卷动到最近有效位置。2 边距的默认值为 50 像素。

[override virtual protected] bool QScrollArea:: event ( QEvent * e )

重实现: QAbstractScrollArea::event (QEvent *event).

[override virtual protected] bool QScrollArea:: eventFilter ( QObject * o , QEvent * e )

重实现: QObject::eventFilter (QObject *watched, QEvent *event).

[override virtual] bool QScrollArea:: focusNextPrevChild ( bool next )

重实现: QWidget::focusNextPrevChild (bool next).

[override virtual protected] void QScrollArea:: resizeEvent ( QResizeEvent *)

重实现: QAbstractScrollArea::resizeEvent (QResizeEvent *event).

[override virtual protected] void QScrollArea:: scrollContentsBy ( int dx , int dy )

重实现: QAbstractScrollArea::scrollContentsBy (int dx, int dy).

void QScrollArea:: setWidget ( QWidget * widget )

设置卷动区域 widget .

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.

Widget 的 autoFillBackground 特性会被设为 true .

若卷动区域可见当 widget 有添加,必须 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 show() the scroll area. In this case, you can also not show() widget later.

另请参阅 widget ().

[override virtual] QSize QScrollArea:: sizeHint () const

重实现: QAbstractScrollArea::sizeHint () const.

QWidget *QScrollArea:: takeWidget ()

移除卷动区域 Widget,并将 Widget 的所有权传递给调用者。

另请参阅 widget ().

[override virtual protected] QSize QScrollArea:: viewportSizeHint () const

重实现: QAbstractScrollArea::viewportSizeHint () const.

QWidget *QScrollArea:: widget () const

返回卷动区域 Widget,或 nullptr 若没有。

另请参阅 setWidget ().