内容表

上一话题

QAbstractItemView

下一话题

QAbstractSlider

QAbstractScrollArea

QAbstractScrollArea widget provides a scrolling area with on-demand scroll bars. 更多

Inheritance diagram of PySide2.QtWidgets.QAbstractScrollArea

继承者: QtCharts.QChartView , QHelpContentWidget , QHelpIndexWidget , QAbstractItemView , QColumnView , QGraphicsView , QHeaderView , QListView , QListWidget , QMdiArea , QPlainTextEdit , QScrollArea , QTableView , QTableWidget , QTextBrowser , QTextEdit , QTreeView , QTreeWidget , QUndoView

详细描述

QAbstractScrollArea is a low-level abstraction of a scrolling area. The area provides a central widget called the viewport, in which the contents of the area is to be scrolled (i.e, the visible parts of the contents are rendered in the viewport).

Next to the viewport is a vertical scroll bar, and below is a horizontal scroll bar. When all of the area contents fits in the viewport, each scroll bar can be either visible or hidden depending on the scroll bar’s ScrollBarPolicy . When a scroll bar is hidden, the viewport expands in order to cover all available space. When a scroll bar becomes visible again, the viewport shrinks in order to make room for the scroll bar.

It is possible to reserve a margin area around the viewport, see setViewportMargins() . The feature is mostly used to place a QHeaderView widget above or beside the scrolling area. Subclasses of QAbstractScrollArea should implement margins.

When inheriting QAbstractScrollArea , you need to do the following:

  • Control the scroll bars by setting their range, value, page step, and tracking their movements.

  • Draw the contents of the area in the viewport according to the values of the scroll bars.

  • Handle events received by the viewport in viewportEvent() - notably resize events.

  • 使用 viewport->update() to update the contents of the viewport instead of update() 因为所有描绘操作都发生在视口中。

With a scroll bar policy of ScrollBarAsNeeded (默认), QAbstractScrollArea shows scroll bars when they provide a non-zero scrolling range, and hides them otherwise.

The scroll bars and viewport should be updated whenever the viewport receives a resize event or the size of the contents changes. The viewport also needs to be updated when the scroll bars values change. The initial values of the scroll bars are often set when the area receives new contents.

We give a simple example, in which we have implemented a scroll area that can scroll any QWidget . We make the widget a child of the viewport; this way, we do not have to calculate which part of the widget to draw but can simply move the widget with move() . When the area contents or the viewport size changes, we do the following:

areaSize = viewport().size()
widgetSize = widget.size()
self.verticalScrollBar().setPageStep(widgetSize.height())
self.horizontalScrollBar().setPageStep(widgetSize.width())
self.verticalScrollBar().setRange(0, widgetSize.height() - areaSize.height())
self.horizontalScrollBar().setRange(0, widgetSize.width() - areaSize.width())
self.updateWidgetPosition()
											

When the scroll bars change value, we need to update the widget position, i.e., find the part of the widget that is to be drawn in the viewport:

hvalue = self.horizontalScrollBar().value()
vvalue = self.verticalScrollBar().value()
topLeft = self.viewport().rect().topLeft()
self.widget.move(topLeft.x() - hvalue, topLeft.y() - vvalue)
											

In order to track scroll bar movements, reimplement the virtual function scrollContentsBy() . In order to fine-tune scrolling behavior, connect to a scroll bar’s actionTriggered() signal and adjust the sliderPosition as you wish.

For convenience, QAbstractScrollArea makes all viewport events available in the virtual viewportEvent() handler. QWidget ‘s specialized handlers are remapped to viewport events in the cases where this makes sense. The remapped specialized handlers are: paintEvent() , mousePressEvent() , mouseReleaseEvent() , mouseDoubleClickEvent() , mouseMoveEvent() , wheelEvent() , dragEnterEvent() , dragMoveEvent() , dragLeaveEvent() , dropEvent() , contextMenuEvent() ,和 resizeEvent() .

QScrollArea , which inherits QAbstractScrollArea , provides smooth scrolling for any QWidget (i.e., the widget is scrolled pixel by pixel). You only need to subclass QAbstractScrollArea if you need more specialized behavior. This is, for instance, true if the entire contents of the area is not suitable for being drawn on a QWidget or if you do not want smooth scrolling.

另请参阅

QScrollArea

class QAbstractScrollArea ( [ parent=None ] )
param parent

QWidget

Constructs a viewport.

parent 自变量被发送给 QWidget 构造函数。

PySide2.QtWidgets.QAbstractScrollArea. SizeAdjustPolicy

此枚举指定尺寸提示应如何 QAbstractScrollArea should adjust when the size of the viewport changes.

常量

描述

QAbstractScrollArea.AdjustIgnored

The scroll area will behave like before - and not do any adjust.

QAbstractScrollArea.AdjustToContents

The scroll area will always adjust to the viewport

QAbstractScrollArea.AdjustToContentsOnFirstShow

The scroll area will adjust to its viewport the first time it is shown.

PySide2.QtWidgets.QAbstractScrollArea. addScrollBarWidget ( widget , alignment )
参数

添加 widget as a scroll bar widget in the location specified by alignment .

Scroll bar widgets are shown next to the horizontal or vertical scroll bar, and can be placed on either side of it. If you want the scroll bar widgets to be always visible, set the scrollBarPolicy for the corresponding scroll bar to AlwaysOn .

alignment must be one of Qt::Alignleft and AlignRight , which maps to the horizontal scroll bar, or AlignTop and AlignBottom , which maps to the vertical scroll bar.

A scroll bar widget can be removed by either re-parenting the widget or deleting it. It’s also possible to hide a widget with hide()

The scroll bar widget will be resized to fit the scroll bar geometry for the current style. The following describes the case for scroll bar widgets on the horizontal scroll bar:

The height of the widget will be set to match the height of the scroll bar. To control the width of the widget, use setMinimumWidth and setMaximumWidth , or implement sizeHint() and set a horizontal size policy. If you want a square widget, call pixelMetric ( PM_ScrollBarExtent ) and set the width to this value.

PySide2.QtWidgets.QAbstractScrollArea. cornerWidget ( )
返回类型

QWidget

Returns the widget in the corner between the two scroll bars.

By default, no corner widget is present.

另请参阅

setCornerWidget()

PySide2.QtWidgets.QAbstractScrollArea. horizontalScrollBar ( )
返回类型

QScrollBar

Returns the horizontal scroll bar.

PySide2.QtWidgets.QAbstractScrollArea. horizontalScrollBarPolicy ( )
返回类型

ScrollBarPolicy

PySide2.QtWidgets.QAbstractScrollArea. maximumViewportSize ( )
返回类型

QSize

Returns the size of the viewport as if the scroll bars had no valid scrolling range.

PySide2.QtWidgets.QAbstractScrollArea. scrollBarWidgets ( alignment )
参数

alignment Alignment

返回类型

Returns a list of the currently set scroll bar widgets. alignment can be any combination of the four location flags.

PySide2.QtWidgets.QAbstractScrollArea. scrollContentsBy ( dx , dy )
参数
  • dx int

  • dy int

调用此虚拟处理程序当滚动条被移动按 dx , dy , and consequently the viewport’s contents should be scrolled accordingly.

默认实现只需调用 update() on the entire viewport() , subclasses can reimplement this handler for optimization purposes, or - like QScrollArea - to move a contents widget. The parameters dx and dy are there for convenience, so that the class knows how much should be scrolled (useful e.g. when doing pixel-shifts). You may just as well ignore these values and scroll directly to the position the scroll bars indicate.

Calling this function in order to scroll programmatically is an error, use the scroll bars instead (e.g. by calling setValue() directly).

PySide2.QtWidgets.QAbstractScrollArea. setCornerWidget ( widget )
参数

widget QWidget

Sets the widget in the corner between the two scroll bars to be widget .

You will probably also want to set at least one of the scroll bar modes to AlwaysOn .

传递 None 不展示角落 Widget。

Any previous corner widget is hidden.

You may call with the same widget at different times.

All widgets set here will be deleted by the scroll area when it is destroyed unless you separately reparent the widget after setting some other corner widget (or None ).

Any newly set widget should have no current parent.

By default, no corner widget is present.

PySide2.QtWidgets.QAbstractScrollArea. setHorizontalScrollBar ( scrollbar )
参数

scrollbar QScrollBar

Replaces the existing horizontal scroll bar with scrollBar , and sets all the former scroll bar’s slider properties on the new scroll bar. The former scroll bar is then deleted.

QAbstractScrollArea already provides horizontal and vertical scroll bars by default. You can call this function to replace the default horizontal scroll bar with your own custom scroll bar.

PySide2.QtWidgets.QAbstractScrollArea. setHorizontalScrollBarPolicy ( arg__1 )
参数

arg__1 ScrollBarPolicy

PySide2.QtWidgets.QAbstractScrollArea. setSizeAdjustPolicy ( policy )
参数

policy SizeAdjustPolicy

PySide2.QtWidgets.QAbstractScrollArea. setVerticalScrollBar ( scrollbar )
参数

scrollbar QScrollBar

Replaces the existing vertical scroll bar with scrollBar , and sets all the former scroll bar’s slider properties on the new scroll bar. The former scroll bar is then deleted.

QAbstractScrollArea already provides vertical and horizontal scroll bars by default. You can call this function to replace the default vertical scroll bar with your own custom scroll bar.

PySide2.QtWidgets.QAbstractScrollArea. setVerticalScrollBarPolicy ( arg__1 )
参数

arg__1 ScrollBarPolicy

PySide2.QtWidgets.QAbstractScrollArea. setViewport ( widget )
参数

widget QWidget

Sets the viewport to be the given widget QAbstractScrollArea will take ownership of the given widget .

widget is None , QAbstractScrollArea will assign a new QWidget instance for the viewport.

另请参阅

viewport()

PySide2.QtWidgets.QAbstractScrollArea. setViewportMargins ( margins )
参数

margins QMargins

margins around the scrolling area. This is useful for applications such as spreadsheets with “locked” rows and columns. The marginal space is is left blank; put widgets in the unused area.

By default all margins are zero.

另请参阅

viewportMargins()

PySide2.QtWidgets.QAbstractScrollArea. setViewportMargins ( left , top , right , bottom )
参数
  • left int

  • top int

  • right int

  • bottom int

Sets the margins around the scrolling area to left , top , right and bottom . This is useful for applications such as spreadsheets with “locked” rows and columns. The marginal space is is left blank; put widgets in the unused area.

Note that this function is frequently called by QTreeView and QTableView , so margins must be implemented by QAbstractScrollArea subclasses. Also, if the subclasses are to be used in item views, they should not call this function.

By default all margins are zero.

另请参阅

viewportMargins()

PySide2.QtWidgets.QAbstractScrollArea. setupViewport ( 视口 )
参数

视口 QWidget

This slot is called by QAbstractScrollArea after setViewport ( 视口 ) has been called. Reimplement this function in a subclass of QAbstractScrollArea to initialize the new 视口 before it is used.

另请参阅

setViewport()

PySide2.QtWidgets.QAbstractScrollArea. sizeAdjustPolicy ( )
返回类型

SizeAdjustPolicy

PySide2.QtWidgets.QAbstractScrollArea. verticalScrollBar ( )
返回类型

QScrollBar

返回垂直滚动条。

PySide2.QtWidgets.QAbstractScrollArea. verticalScrollBarPolicy ( )
返回类型

ScrollBarPolicy

PySide2.QtWidgets.QAbstractScrollArea. 视口 ( )
返回类型

QWidget

返回视口 Widget。

使用 widget() function to retrieve the contents of the viewport widget.

PySide2.QtWidgets.QAbstractScrollArea. viewportEvent ( arg__1 )
参数

arg__1 QEvent

返回类型

bool

The main event handler for the scrolling area (the viewport() widget). It handles the event specified, and can be called by subclasses to provide reasonable default behavior.

返回 true to indicate to the event system that the event has been handled, and needs no further processing; otherwise returns false to indicate that the event should be propagated further.

You can reimplement this function in a subclass, but we recommend using one of the specialized event handlers instead.

Specialized handlers for viewport events are: paintEvent() , mousePressEvent() , mouseReleaseEvent() , mouseDoubleClickEvent() , mouseMoveEvent() , wheelEvent() , dragEnterEvent() , dragMoveEvent() , dragLeaveEvent() , dropEvent() , contextMenuEvent() ,和 resizeEvent() .

PySide2.QtWidgets.QAbstractScrollArea. viewportMargins ( )
返回类型

QMargins

Returns the margins around the scrolling area. By default all the margins are zero.

PySide2.QtWidgets.QAbstractScrollArea. viewportSizeHint ( )
返回类型

QSize

Returns the recommended size for the viewport. The default implementation returns viewport() -> sizeHint() . Note that the size is just the viewport’s size, without any scroll bars visible.