PySide6.QtWidgets.QAbstractScrollArea¶
- class QAbstractScrollArea¶
- The - QAbstractScrollAreawidget provides a scrolling area with on-demand scroll bars. More…- Inherited by: - QTextEdit,- QTextBrowser,- QScrollArea,- QPlainTextEdit,- QMdiArea,- QGraphicsView,- QAbstractItemView,- QTreeView,- QTreeWidget,- QHelpContentWidget,- QTableView,- QTableWidget,- QListView,- QUndoView,- QListWidget,- QHelpIndexWidget,- QHeaderView,- QColumnView,- QPdfView,- QChartView- Synopsis¶- Properties¶- horizontalScrollBarPolicyᅟ- The policy for the horizontal scroll bar
- sizeAdjustPolicyᅟ- The policy describing how the size of the scroll area changes when the size of the viewport changes
- verticalScrollBarPolicyᅟ- The policy for the vertical scroll bar
 - Methods¶- def - __init__()
- def - cornerWidget()
- def - setViewport()
- def - viewport()
 - Virtual methods¶
- def - setupViewport()
- def - viewportEvent()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - QAbstractScrollAreais 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 Qt::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- QHeaderViewwidget above or beside the scrolling area. Subclasses of- QAbstractScrollAreashould 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.
- Use - viewport->update()to update the contents of the viewport instead of- update()as all painting operations take place on the viewport.
 - With a scroll bar policy of Qt::ScrollBarAsNeeded (the default), - QAbstractScrollAreashows 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() QSize widgetSize = widget.size() verticalScrollBar().setPageStep(areaSize.height()) horizontalScrollBar().setPageStep(areaSize.width()) verticalScrollBar().setRange(0, widgetSize.height() - areaSize.height()) horizontalScrollBar().setRange(0, widgetSize.width() - areaSize.width()) 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 = horizontalScrollBar().value() vvalue = verticalScrollBar().value() topLeft = viewport().rect().topLeft() 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- sliderPositionas you wish.- For convenience, - QAbstractScrollAreamakes 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(), and- 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- QAbstractScrollAreaif 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- QWidgetor if you do not want smooth scrolling.- See also - class SizeAdjustPolicy¶
- This enum specifies how the size hint of the - QAbstractScrollAreashould adjust when the size of the viewport changes.- Constant - Description - 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. 
 - Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property horizontalScrollBarPolicyᅟ: Qt.ScrollBarPolicy¶
 - This property holds the policy for the horizontal scroll bar. - The default policy is Qt::ScrollBarAsNeeded. - See also - Access functions:
 - property sizeAdjustPolicyᅟ: QAbstractScrollArea.SizeAdjustPolicy¶
 - This property holds the policy describing how the size of the scroll area changes when the size of the viewport changes.. - The default policy is - AdjustIgnored. Changing this property might actually resize the scrollarea.- Access functions:
 - property verticalScrollBarPolicyᅟ: Qt.ScrollBarPolicy¶
 - This property holds the policy for the vertical scroll bar. - The default policy is Qt::ScrollBarAsNeeded. - See also - Access functions:
 - Constructs a viewport. - The - parentargument is sent to the- QWidgetconstructor.- addScrollBarWidget(widget, alignment)¶
- Parameters:
- widget – - QWidget
- alignment – Combination of - AlignmentFlag
 
 
 - Adds - widgetas 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.- alignmentmust be one of Qt::Alignleft and Qt::AlignRight, which maps to the horizontal scroll bar, or Qt::AlignTop and Qt::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 - setMinimumWidthand- 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.- See also - Returns the widget in the corner between the two scroll bars. - By default, no corner widget is present. - See also - horizontalScrollBar()¶
- Return type:
 
 - Returns the horizontal scroll bar. - horizontalScrollBarPolicy()¶
- Return type:
 - See also 
 - Getter of property - horizontalScrollBarPolicyᅟ.- Returns the size of the viewport as if the scroll bars had no valid scrolling range. - scrollBarWidgets(alignment)¶
- Parameters:
- alignment – Combination of - AlignmentFlag
- Return type:
- .list of QWidget 
 
 - Returns a list of the currently set scroll bar widgets. - alignmentcan be any combination of the four location flags.- See also - scrollContentsBy(dx, dy)¶
- Parameters:
- dx – int 
- dy – int 
 
 
 - This virtual handler is called when the scroll bars are moved by - dx,- dy, and consequently the viewport’s contents should be scrolled accordingly.- The default implementation simply calls - update()on the entire- viewport(), subclasses can reimplement this handler for optimization purposes, or - like- QScrollArea- to move a contents widget. The parameters- dxand- dyare 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).- 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.- Passing - Noneshows no widget in the corner.- Any previous corner widget is hidden. - You may call setCornerWidget() 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. - setHorizontalScrollBar(scrollbar)¶
- Parameters:
- 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.- QAbstractScrollAreaalready 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.- setHorizontalScrollBarPolicy(arg__1)¶
- Parameters:
- arg__1 – - ScrollBarPolicy
 - See also 
 - Setter of property - horizontalScrollBarPolicyᅟ.- setSizeAdjustPolicy(policy)¶
- Parameters:
- policy – - SizeAdjustPolicy
 - See also 
 - Setter of property - sizeAdjustPolicyᅟ.- setVerticalScrollBar(scrollbar)¶
- Parameters:
- 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.- QAbstractScrollAreaalready 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.- setVerticalScrollBarPolicy(arg__1)¶
- Parameters:
- arg__1 – - ScrollBarPolicy
 - See also 
 - Setter of property - verticalScrollBarPolicyᅟ.- Sets the viewport to be the given - widget. The- QAbstractScrollAreawill take ownership of the given- widget.- If - widgetis- None,- QAbstractScrollAreawill assign a new- QWidgetinstance for the viewport.- See also - Sets - marginsaround 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. - See also - setViewportMargins(left, top, right, bottom)
- Parameters:
- left – int 
- top – int 
- right – int 
- bottom – int 
 
 
 - Sets the margins around the scrolling area to - left,- top,- rightand- bottom. This is useful for applications such as spreadsheets with “locked” rows and columns. The marginal space is left blank; put widgets in the unused area.- Note that this function is frequently called by - QTreeViewand- QTableView, so margins must be implemented by- QAbstractScrollAreasubclasses. Also, if the subclasses are to be used in item views, they should not call this function.- By default all margins are zero. - See also - This slot is called by - QAbstractScrollAreaafter- setViewport(- viewport) has been called. Reimplement this function in a subclass of- QAbstractScrollAreato initialize the new- viewportbefore it is used.- See also - sizeAdjustPolicy()¶
- Return type:
 - See also 
 - Getter of property - sizeAdjustPolicyᅟ.- verticalScrollBar()¶
- Return type:
 
 - Returns the vertical scroll bar. - verticalScrollBarPolicy()¶
- Return type:
 - See also 
 - Getter of property - verticalScrollBarPolicyᅟ.- Returns the viewport widget. - Use the - widget()function to retrieve the contents of the viewport widget.- See also - The main event handler for the scrolling area (the - viewport()widget). It handles the- eventspecified, and can be called by subclasses to provide reasonable default behavior.- Returns - trueto indicate to the event system that the event has been handled, and needs no further processing; otherwise returns- falseto 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(), and- resizeEvent().- Returns the margins around the scrolling area. By default all the margins are zero. - See also - 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.