QAbstractScrollArea

Inheritance diagram of QAbstractScrollArea

Inherited by: QGraphicsView, QDeclarativeView, QScrollArea, QPlainTextEdit, QTextEdit, QTextBrowser, QMdiArea, QAbstractItemView, QHeaderView, QTreeView, QTreeWidget, QHelpContentWidget, QColumnView, QTableView, QTableWidget, QListView, QHelpIndexWidget, QUndoView, QListWidget

Synopsis

Functions

Virtual functions

Slots

Detailed Description

The PySide.QtGui.QAbstractScrollArea widget provides a scrolling area with on-demand scroll bars.

PySide.QtGui.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 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 PySide.QtGui.QAbstractScrollArea.setViewportMargins() . The feature is mostly used to place a PySide.QtGui.QHeaderView widget above or beside the scrolling area. Subclasses of PySide.QtGui.QAbstractScrollArea should implement margins.

When inheriting PySide.QtGui.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 PySide.QtGui.QAbstractScrollArea.viewportEvent() - notably resize events.
  • Use viewport->update() to update the contents of the viewport instead of PySide.QtGui.QWidget.update() as all painting operations take place on the viewport.

With a scroll bar policy of Qt.ScrollBarAsNeeded (the default), PySide.QtGui.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 PySide.QtGui.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 QWidget.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 PySide.QtGui.QAbstractScrollArea.scrollContentsBy() . In order to fine-tune scrolling behavior, connect to a scroll bar’s QAbstractSlider.actionTriggered() signal and adjust the QAbstractSlider.sliderPosition as you wish.

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

PySide.QtGui.QScrollArea , which inherits PySide.QtGui.QAbstractScrollArea , provides smooth scrolling for any PySide.QtGui.QWidget (i.e., the widget is scrolled pixel by pixel). You only need to subclass PySide.QtGui.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 PySide.QtGui.QWidget or if you do not want smooth scrolling.

class PySide.QtGui.QAbstractScrollArea([parent=None])
Parameters:parentPySide.QtGui.QWidget

Constructs a viewport.

The parent arguments is sent to the PySide.QtGui.QWidget constructor.

PySide.QtGui.QAbstractScrollArea.addScrollBarWidget(widget, alignment)
Parameters:
PySide.QtGui.QAbstractScrollArea.cornerWidget()
Return type:PySide.QtGui.QWidget

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

By default, no corner widget is present.

PySide.QtGui.QAbstractScrollArea.horizontalScrollBar()
Return type:PySide.QtGui.QScrollBar

Returns the horizontal scroll bar.

PySide.QtGui.QAbstractScrollArea.horizontalScrollBarPolicy()
Return type:PySide.QtCore.Qt.ScrollBarPolicy

This property holds the policy for the horizontal scroll bar.

The default policy is Qt.ScrollBarAsNeeded .

PySide.QtGui.QAbstractScrollArea.maximumViewportSize()
Return type:PySide.QtCore.QSize

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

PySide.QtGui.QAbstractScrollArea.scrollBarWidgets(alignment)
Parameters:alignmentPySide.QtCore.Qt.Alignment
Return type:
PySide.QtGui.QAbstractScrollArea.scrollContentsBy(dx, dy)
Parameters:
  • dxPySide.QtCore.int
  • dyPySide.QtCore.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 PySide.QtGui.QWidget.update() on the entire PySide.QtGui.QAbstractScrollArea.viewport() , subclasses can reimplement this handler for optimization purposes, or - like PySide.QtGui.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 QScrollBar.setValue() directly).

PySide.QtGui.QAbstractScrollArea.setCornerWidget(widget)
Parameters:widgetPySide.QtGui.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 .

Passing 0 shows no widget in the corner.

Any previous corner widget is hidden.

You may call PySide.QtGui.QAbstractScrollArea.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 0).

Any newly set widget should have no current parent.

By default, no corner widget is present.

PySide.QtGui.QAbstractScrollArea.setHorizontalScrollBar(scrollbar)
Parameters:scrollbarPySide.QtGui.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.

PySide.QtGui.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.

PySide.QtGui.QAbstractScrollArea.setHorizontalScrollBarPolicy(arg__1)
Parameters:arg__1PySide.QtCore.Qt.ScrollBarPolicy

This property holds the policy for the horizontal scroll bar.

The default policy is Qt.ScrollBarAsNeeded .

PySide.QtGui.QAbstractScrollArea.setVerticalScrollBar(scrollbar)
Parameters:scrollbarPySide.QtGui.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.

PySide.QtGui.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.

PySide.QtGui.QAbstractScrollArea.setVerticalScrollBarPolicy(arg__1)
Parameters:arg__1PySide.QtCore.Qt.ScrollBarPolicy

This property holds the policy for the vertical scroll bar.

The default policy is Qt.ScrollBarAsNeeded .

PySide.QtGui.QAbstractScrollArea.setViewport(widget)
Parameters:widgetPySide.QtGui.QWidget

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

If widget is 0, PySide.QtGui.QAbstractScrollArea will assign a new PySide.QtGui.QWidget instance for the viewport.

PySide.QtGui.QAbstractScrollArea.setViewportMargins(left, top, right, bottom)
Parameters:
  • leftPySide.QtCore.int
  • topPySide.QtCore.int
  • rightPySide.QtCore.int
  • bottomPySide.QtCore.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 PySide.QtGui.QTreeView and PySide.QtGui.QTableView , so margins must be implemented by PySide.QtGui.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.

PySide.QtGui.QAbstractScrollArea.setViewportMargins(margins)
Parameters:marginsPySide.QtCore.QMargins

Sets 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.

PySide.QtGui.QAbstractScrollArea.setupViewport(viewport)
Parameters:viewportPySide.QtGui.QWidget

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

PySide.QtGui.QAbstractScrollArea.verticalScrollBar()
Return type:PySide.QtGui.QScrollBar

Returns the vertical scroll bar.

PySide.QtGui.QAbstractScrollArea.verticalScrollBarPolicy()
Return type:PySide.QtCore.Qt.ScrollBarPolicy

This property holds the policy for the vertical scroll bar.

The default policy is Qt.ScrollBarAsNeeded .

PySide.QtGui.QAbstractScrollArea.viewport()
Return type:PySide.QtGui.QWidget

Returns the viewport widget.

Use the QScrollArea.widget() function to retrieve the contents of the viewport widget.

PySide.QtGui.QAbstractScrollArea.viewportEvent(arg__1)
Parameters:arg__1PySide.QtCore.QEvent
Return type:PySide.QtCore.bool

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

Returns 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: PySide.QtGui.QAbstractScrollArea.paintEvent() , PySide.QtGui.QAbstractScrollArea.mousePressEvent() , PySide.QtGui.QAbstractScrollArea.mouseReleaseEvent() , PySide.QtGui.QAbstractScrollArea.mouseDoubleClickEvent() , PySide.QtGui.QAbstractScrollArea.mouseMoveEvent() , PySide.QtGui.QAbstractScrollArea.wheelEvent() , PySide.QtGui.QAbstractScrollArea.dragEnterEvent() , PySide.QtGui.QAbstractScrollArea.dragMoveEvent() , PySide.QtGui.QAbstractScrollArea.dragLeaveEvent() , PySide.QtGui.QAbstractScrollArea.dropEvent() , PySide.QtGui.QAbstractScrollArea.contextMenuEvent() , and PySide.QtGui.QAbstractScrollArea.resizeEvent() .