QGraphicsView

The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene . More

Inheritance diagram of PySide2.QtWidgets.QGraphicsView

Inherited by: QChartView

Synopsis

Functions

Virtual functions

Slots

Signals

Detailed Description

QGraphicsView visualizes the contents of a QGraphicsScene in a scrollable viewport. To create a scene with geometrical items, see QGraphicsScene ‘s documentation. QGraphicsView is part of the Graphics View Framework .

To visualize a scene, you start by constructing a QGraphicsView object, passing the address of the scene you want to visualize to QGraphicsView ‘s constructor. Alternatively, you can call setScene() to set the scene at a later point. After you call show() , the view will by default scroll to the center of the scene and display any items that are visible at this point. For example:

scene = QGraphicsScene()
scene.addText("Hello, world!")

view = QGraphicsView(scene)
view.show()

You can explicitly scroll to any position on the scene by using the scroll bars, or by calling centerOn() . By passing a point to centerOn() , QGraphicsView will scroll its viewport to ensure that the point is centered in the view. An overload is provided for scrolling to a QGraphicsItem , in which case QGraphicsView will see to that the center of the item is centered in the view. If all you want is to ensure that a certain area is visible, (but not necessarily centered,) you can call ensureVisible() instead.

QGraphicsView can be used to visualize a whole scene, or only parts of it. The visualized area is by default detected automatically when the view is displayed for the first time (by calling itemsBoundingRect() ). To set the visualized area rectangle yourself, you can call setSceneRect() . This will adjust the scroll bars’ ranges appropriately. Note that although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX).

QGraphicsView visualizes the scene by calling render() . By default, the items are drawn onto the viewport by using a regular QPainter , and using default render hints. To change the default render hints that QGraphicsView passes to QPainter when painting items, you can call setRenderHints() .

By default, QGraphicsView provides a regular QWidget for the viewport widget. You can access this widget by calling viewport() , or you can replace it by calling setViewport() . To render using OpenGL, simply call setViewport (new QOpenGLWidget ). QGraphicsView takes ownership of the viewport widget.

QGraphicsView supports affine transformations, using QTransform . You can either pass a matrix to setTransform() , or you can call one of the convenience functions rotate() , scale() , translate() or shear() . The most two common transformations are scaling, which is used to implement zooming, and rotation. QGraphicsView keeps the center of the view fixed during a transformation. Because of the scene alignment (setAligment()), translating the view will have no visual impact.

You can interact with the items on the scene by using the mouse and keyboard. QGraphicsView translates the mouse and key events into scene events, (events that inherit QGraphicsSceneEvent ,), and forward them to the visualized scene. In the end, it’s the individual item that handles the events and reacts to them. For example, if you click on a selectable item, the item will typically let the scene know that it has been selected, and it will also redraw itself to display a selection rectangle. Similiary, if you click and drag the mouse to move a movable item, it’s the item that handles the mouse moves and moves itself. Item interaction is enabled by default, and you can toggle it by calling setInteractive() .

You can also provide your own custom scene interaction, by creating a subclass of QGraphicsView , and reimplementing the mouse and key event handlers. To simplify how you programmatically interact with items in the view, QGraphicsView provides the mapping functions mapToScene() and mapFromScene() , and the item accessors items() and itemAt() . These functions allow you to map points, rectangles, polygons and paths between view coordinates and scene coordinates, and to find items on the scene using view coordinates.

../../_images/graphicsview-view.png

Note

Using an OpenGL viewport limits the ability to use QGraphicsProxyWidget . Not all combinations of widgets and styles can be supported with such a setup. You should carefully test your UI and make the necessary adjustments.

class PySide2.QtWidgets.QGraphicsView(scene[, parent=None])

PySide2.QtWidgets.QGraphicsView([parent=None])

param parent

PySide2.QtWidgets.QWidget

param scene

PySide2.QtWidgets.QGraphicsScene

Constructs a QGraphicsView and sets the visualized scene to scene . parent is passed to QWidget ‘s constructor.

Constructs a QGraphicsView . parent is passed to QWidget ‘s constructor.

PySide2.QtWidgets.QGraphicsView.ViewportAnchor

This enums describe the possible anchors that QGraphicsView can use when the user resizes the view or when the view is transformed.

Constant

Description

QGraphicsView.NoAnchor

No anchor, i.e. the view leaves the scene’s position unchanged.

QGraphicsView.AnchorViewCenter

The scene point at the center of the view is used as the anchor.

QGraphicsView.AnchorUnderMouse

The point under the mouse is used as the anchor.

PySide2.QtWidgets.QGraphicsView.CacheModeFlag

This enum describes the flags that you can set for a QGraphicsView ‘s cache mode.

Constant

Description

QGraphicsView.CacheNone

All painting is done directly onto the viewport.

QGraphicsView.CacheBackground

The background is cached. This affects both custom backgrounds, and backgrounds based on the backgroundBrush property. When this flag is enabled, QGraphicsView will allocate one pixmap with the full size of the viewport.

See also

cacheMode

PySide2.QtWidgets.QGraphicsView.DragMode

This enum describes the default action for the view when pressing and dragging the mouse over the viewport.

Constant

Description

QGraphicsView.NoDrag

Nothing happens; the mouse event is ignored.

QGraphicsView.ScrollHandDrag

The cursor changes into a pointing hand, and dragging the mouse around will scroll the scrolbars. This mode works both in interactive and non-interactive mode.

QGraphicsView.RubberBandDrag

A rubber band will appear. Dragging the mouse will set the rubber band geometry, and all items covered by the rubber band are selected. This mode is disabled for non-interactive views.

PySide2.QtWidgets.QGraphicsView.ViewportUpdateMode

This enum describes how QGraphicsView updates its viewport when the scene contents change or are exposed.

Constant

Description

QGraphicsView.FullViewportUpdate

When any visible part of the scene changes or is reexposed, QGraphicsView will update the entire viewport. This approach is fastest when QGraphicsView spends more time figuring out what to draw than it would spend drawing (e.g., when very many small items are repeatedly updated). This is the preferred update mode for viewports that do not support partial updates, such as QOpenGLWidget , and for viewports that need to disable scroll optimization.

QGraphicsView.MinimalViewportUpdate

QGraphicsView will determine the minimal viewport region that requires a redraw, minimizing the time spent drawing by avoiding a redraw of areas that have not changed. This is QGraphicsView ‘s default mode. Although this approach provides the best performance in general, if there are many small visible changes on the scene, QGraphicsView might end up spending more time finding the minimal approach than it will spend drawing.

QGraphicsView.SmartViewportUpdate

QGraphicsView will attempt to find an optimal update mode by analyzing the areas that require a redraw.

QGraphicsView.BoundingRectViewportUpdate

The bounding rectangle of all changes in the viewport will be redrawn. This mode has the advantage that QGraphicsView searches only one region for changes, minimizing time spent determining what needs redrawing. The disadvantage is that areas that have not changed also need to be redrawn.

QGraphicsView.NoViewportUpdate

QGraphicsView will never update its viewport when the scene changes; the user is expected to control all updates. This mode disables all (potentially slow) item visibility testing in QGraphicsView , and is suitable for scenes that either require a fixed frame rate, or where the viewport is otherwise updated externally.

PySide2.QtWidgets.QGraphicsView.OptimizationFlag

This enum describes flags that you can enable to improve rendering performance in QGraphicsView . By default, none of these flags are set. Note that setting a flag usually imposes a side effect, and this effect can vary between paint devices and platforms.

Constant

Description

QGraphicsView.DontClipPainter

This value is obsolete and has no effect.

QGraphicsView.DontSavePainterState

When rendering, QGraphicsView protects the painter state (see save() ) when rendering the background or foreground, and when rendering each item. This allows you to leave the painter in an altered state (i.e., you can call setPen() or setBrush() without restoring the state after painting). However, if the items consistently do restore the state, you should enable this flag to prevent QGraphicsView from doing the same.

QGraphicsView.DontAdjustForAntialiasing

Disables QGraphicsView ‘s antialiasing auto-adjustment of exposed areas. Items that render antialiased lines on the boundaries of their boundingRect() can end up rendering parts of the line outside. To prevent rendering artifacts, QGraphicsView expands all exposed regions by 2 pixels in all directions. If you enable this flag, QGraphicsView will no longer perform these adjustments, minimizing the areas that require redrawing, which improves performance. A common side effect is that items that do draw with antialiasing can leave painting traces behind on the scene as they are moved.

QGraphicsView.IndirectPainting

Since Qt 4.6, restore the old painting algorithm that calls drawItems() and drawItems() . To be used only for compatibility with old code.

PySide2.QtWidgets.QGraphicsView.alignment()
Return type

Alignment

This property holds the alignment of the scene in the view when the whole scene is visible..

If the whole scene is visible in the view, (i.e., there are no visible scroll bars,) the view’s alignment will decide where the scene will be rendered in the view. For example, if the alignment is AlignCenter , which is default, the scene will be centered in the view, and if the alignment is ( AlignLeft | AlignTop ), the scene will be rendered in the top-left corner of the view.

PySide2.QtWidgets.QGraphicsView.backgroundBrush()
Return type

PySide2.QtGui.QBrush

This property holds the background brush of the scene..

This property sets the background brush for the scene in this view. It is used to override the scene’s own background, and defines the behavior of drawBackground() . To provide custom background drawing for this view, you can reimplement drawBackground() instead.

By default, this property contains a brush with the NoBrush pattern.

PySide2.QtWidgets.QGraphicsView.cacheMode()
Return type

CacheMode

This property holds which parts of the view are cached.

QGraphicsView can cache pre-rendered content in a QPixmap , which is then drawn onto the viewport. The purpose of such caching is to speed up the total rendering time for areas that are slow to render. Texture, gradient and alpha blended backgrounds, for example, can be notibly slow to render; especially with a transformed view. The CacheBackground flag enables caching of the view’s background. For example:

view = QGraphicsView()
view.setBackgroundBrush(QImage(":/images/backgroundtile.png"))
view.setCacheMode(QGraphicsView.CacheBackground)

The cache is invalidated every time the view is transformed. However, when scrolling, only partial invalidation is required.

By default, nothing is cached.

See also

resetCachedContent() QPixmapCache

PySide2.QtWidgets.QGraphicsView.centerOn(item)
Parameters

itemPySide2.QtWidgets.QGraphicsItem

This is an overloaded function.

Scrolls the contents of the viewport to ensure that item is centered in the view.

See also

ensureVisible()

PySide2.QtWidgets.QGraphicsView.centerOn(pos)
Parameters

posPySide2.QtCore.QPointF

PySide2.QtWidgets.QGraphicsView.centerOn(x, y)
Parameters
  • x – float

  • y – float

This is an overloaded function.

This function is provided for convenience. It’s equivalent to calling centerOn ( QPointF (x , y )).

PySide2.QtWidgets.QGraphicsView.dragMode()
Return type

DragMode

This property holds the behavior for dragging the mouse over the scene while the left mouse button is pressed..

This property defines what should happen when the user clicks on the scene background and drags the mouse (e.g., scrolling the viewport contents using a pointing hand cursor, or selecting multiple items with a rubber band). The default value, NoDrag , does nothing.

This behavior only affects mouse clicks that are not handled by any item. You can define a custom behavior by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent() .

PySide2.QtWidgets.QGraphicsView.drawBackground(painter, rect)
Parameters

Draws the background of the scene using painter , before any items and the foreground are drawn. Reimplement this function to provide a custom background for this view.

If all you want is to define a color, texture or gradient for the background, you can call setBackgroundBrush() instead.

All painting is done in scene coordinates. rect is the exposed rectangle.

The default implementation fills rect using the view’s backgroundBrush . If no such brush is defined (the default), the scene’s function is called instead.

PySide2.QtWidgets.QGraphicsView.drawForeground(painter, rect)
Parameters

Draws the foreground of the scene using painter , after the background and all items are drawn. Reimplement this function to provide a custom foreground for this view.

If all you want is to define a color, texture or gradient for the foreground, you can call setForegroundBrush() instead.

All painting is done in scene coordinates. rect is the exposed rectangle.

The default implementation fills rect using the view’s foregroundBrush . If no such brush is defined (the default), the scene’s function is called instead.

PySide2.QtWidgets.QGraphicsView.drawItems(painter, items, options)
Parameters

Draws the items items in the scene using painter , after the background and before the foreground are drawn. numItems is the number of items in items and options in options . options is a list of styleoptions; one for each item. Reimplement this function to provide custom item drawing for this view.

The default implementation calls the scene’s function.

Since Qt 4.6, this function is not called anymore unless the IndirectPainting flag is given as an Optimization flag.

See also

drawForeground() drawBackground() drawItems()

PySide2.QtWidgets.QGraphicsView.ensureVisible(item[, xmargin=50[, ymargin=50]])
Parameters

This is an overloaded function.

Scrolls the contents of the viewport so that the center of item item is visible, 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.

See also

centerOn()

PySide2.QtWidgets.QGraphicsView.ensureVisible(rect[, xmargin=50[, ymargin=50]])
Parameters
PySide2.QtWidgets.QGraphicsView.ensureVisible(x, y, w, h[, xmargin=50[, ymargin=50]])
Parameters
  • x – float

  • y – float

  • w – float

  • h – float

  • xmargin – int

  • ymargin – int

This is an overloaded function.

This function is provided for convenience. It’s equivalent to calling ensureVisible ( QRectF (x , y , w , h ), xmargin , ymargin ).

PySide2.QtWidgets.QGraphicsView.fitInView(item[, aspectRadioMode=Qt.IgnoreAspectRatio])
Parameters

This is an overloaded function.

Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode .

PySide2.QtWidgets.QGraphicsView.fitInView(x, y, w, h[, aspectRadioMode=Qt.IgnoreAspectRatio])
Parameters
  • x – float

  • y – float

  • w – float

  • h – float

  • aspectRadioModeAspectRatioMode

This is an overloaded function.

This convenience function is equivalent to calling fitInView ( QRectF (x , y , w , h ), aspectRatioMode ).

PySide2.QtWidgets.QGraphicsView.fitInView(rect[, aspectRadioMode=Qt.IgnoreAspectRatio])
Parameters
PySide2.QtWidgets.QGraphicsView.foregroundBrush()
Return type

PySide2.QtGui.QBrush

This property holds the foreground brush of the scene..

This property sets the foreground brush for the scene in this view. It is used to override the scene’s own foreground, and defines the behavior of drawForeground() . To provide custom foreground drawing for this view, you can reimplement drawForeground() instead.

By default, this property contains a brush with the NoBrush pattern.

PySide2.QtWidgets.QGraphicsView.invalidateScene([rect=QRectF()[, layers=QGraphicsScene.AllLayers]])
Parameters

Invalidates and schedules a redraw of layers inside rect . rect is in scene coordinates. Any cached content for layers inside rect is unconditionally invalidated and redrawn.

You can call this function to notify QGraphicsView of changes to the background or the foreground of the scene. It is commonly used for scenes with tile-based backgrounds to notify changes when QGraphicsView has enabled background caching.

Note that QGraphicsView currently supports background caching only (see CacheBackground ). This function is equivalent to calling update() if any layer but BackgroundLayer is passed.

See also

invalidate() update()

PySide2.QtWidgets.QGraphicsView.isInteractive()
Return type

bool

This property holds whether the view allows scene interaction..

If enabled, this view is set to allow scene interaction. Otherwise, this view will not allow interaction, and any mouse or key events are ignored (i.e., it will act as a read-only view).

By default, this property is true .

PySide2.QtWidgets.QGraphicsView.isTransformed()
Return type

bool

Returns true if the view is transformed (i.e., a non-identity transform has been assigned, or the scrollbars are adjusted).

PySide2.QtWidgets.QGraphicsView.itemAt(pos)
Parameters

posPySide2.QtCore.QPoint

Return type

PySide2.QtWidgets.QGraphicsItem

Returns the item at position pos , which is in viewport coordinates. If there are several items at this position, this function returns the topmost item.

Example:

def mousePressEvent(self, event):
    if (item = itemAt(event.pos()):
        print "You clicked on item", item
    else:
        print "You didn't click on an item."

See also

items() Sorting

PySide2.QtWidgets.QGraphicsView.itemAt(x, y)
Parameters
  • x – int

  • y – int

Return type

PySide2.QtWidgets.QGraphicsItem

This is an overloaded function.

This function is provided for convenience. It’s equivalent to calling itemAt ( QPoint (x , y )).

PySide2.QtWidgets.QGraphicsView.items()
Return type

Returns a list of all the items in the associated scene, in descending stacking order (i.e., the first item in the returned list is the uppermost item).

See also

items() Sorting

PySide2.QtWidgets.QGraphicsView.items(x, y, w, h[, mode=Qt.IntersectsItemShape])
Parameters
Return type

This convenience function is equivalent to calling items( QRectF (x , y , w , h ), mode ).

PySide2.QtWidgets.QGraphicsView.items(x, y)
Parameters
  • x – int

  • y – int

Return type

This function is provided for convenience. It’s equivalent to calling items( QPoint (x , y )).

PySide2.QtWidgets.QGraphicsView.items(rect[, mode=Qt.IntersectsItemShape])
Parameters
Return type

PySide2.QtWidgets.QGraphicsView.items(polygon[, mode=Qt.IntersectsItemShape])
Parameters
Return type

PySide2.QtWidgets.QGraphicsView.items(pos)
Parameters

posPySide2.QtCore.QPoint

Return type

Returns a list of all the items at the position pos in the view. The items are listed in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item). pos is in viewport coordinates.

This function is most commonly called from within mouse event handlers in a subclass in QGraphicsView . pos is in untransformed viewport coordinates, just like pos() .

def mousePressEvent(self, event):
    print "There are", items(event->pos()).size(), "items at position", mapToScene(event->pos())

See also

items() Sorting

PySide2.QtWidgets.QGraphicsView.items(path[, mode=Qt.IntersectsItemShape])
Parameters
Return type

PySide2.QtWidgets.QGraphicsView.mapFromScene(path)
Parameters

pathPySide2.QtGui.QPainterPath

Return type

PySide2.QtGui.QPainterPath

PySide2.QtWidgets.QGraphicsView.mapFromScene(point)
Parameters

pointPySide2.QtCore.QPointF

Return type

PySide2.QtCore.QPoint

PySide2.QtWidgets.QGraphicsView.mapFromScene(polygon)
Parameters

polygonPySide2.QtGui.QPolygonF

Return type

PySide2.QtGui.QPolygon

PySide2.QtWidgets.QGraphicsView.mapFromScene(rect)
Parameters

rectPySide2.QtCore.QRectF

Return type

PySide2.QtGui.QPolygon

PySide2.QtWidgets.QGraphicsView.mapFromScene(x, y)
Parameters
  • x – float

  • y – float

Return type

PySide2.QtCore.QPoint

This function is provided for convenience. It’s equivalent to calling mapFromScene ( QPointF (x , y )).

PySide2.QtWidgets.QGraphicsView.mapFromScene(x, y, w, h)
Parameters
  • x – float

  • y – float

  • w – float

  • h – float

Return type

PySide2.QtGui.QPolygon

This function is provided for convenience. It’s equivalent to calling mapFromScene ( QRectF (x , y , w , h )).

PySide2.QtWidgets.QGraphicsView.mapToScene(polygon)
Parameters

polygonPySide2.QtGui.QPolygon

Return type

PySide2.QtGui.QPolygonF

PySide2.QtWidgets.QGraphicsView.mapToScene(x, y, w, h)
Parameters
  • x – int

  • y – int

  • w – int

  • h – int

Return type

PySide2.QtGui.QPolygonF

This function is provided for convenience. It’s equivalent to calling mapToScene ( QRect (x , y , w , h )).

PySide2.QtWidgets.QGraphicsView.mapToScene(x, y)
Parameters
  • x – int

  • y – int

Return type

PySide2.QtCore.QPointF

This function is provided for convenience. It’s equivalent to calling mapToScene ( QPoint (x , y )).

PySide2.QtWidgets.QGraphicsView.mapToScene(rect)
Parameters

rectPySide2.QtCore.QRect

Return type

PySide2.QtGui.QPolygonF

PySide2.QtWidgets.QGraphicsView.mapToScene(point)
Parameters

pointPySide2.QtCore.QPoint

Return type

PySide2.QtCore.QPointF

PySide2.QtWidgets.QGraphicsView.mapToScene(path)
Parameters

pathPySide2.QtGui.QPainterPath

Return type

PySide2.QtGui.QPainterPath

PySide2.QtWidgets.QGraphicsView.matrix()
Return type

PySide2.QtGui.QMatrix

Note

This function is deprecated.

Use transform() instead.

Returns the current transformation matrix for the view. If no current transformation is set, the identity matrix is returned.

PySide2.QtWidgets.QGraphicsView.optimizationFlags()
Return type

OptimizationFlags

This property holds flags that can be used to tune QGraphicsView ‘s performance..

QGraphicsView uses clipping, extra bounding rect adjustments, and certain other aids to improve rendering quality and performance for the common case graphics scene. However, depending on the target platform, the scene, and the viewport in use, some of these operations can degrade performance.

The effect varies from flag to flag; see the OptimizationFlags documentation for details.

By default, no optimization flags are enabled.

PySide2.QtWidgets.QGraphicsView.render(painter[, target=QRectF()[, source=QRect()[, aspectRatioMode=Qt.KeepAspectRatio]]])
Parameters

Renders the source rect, which is in view coordinates, from the scene into target , which is in paint device coordinates, using painter . This function is useful for capturing the contents of the view onto a paint device, such as a QImage (e.g., to take a screenshot), or for printing to QPrinter . For example:

scene = QGraphicsScene()
scene.addItem(...
...

view = QGraphicsView(scene)
view.show()
...

printer = QPrinter(QPrinter.HighResolution)
printer.setPageSize(QPrinter.A4)
painter = QPainter(printer)

# print, fitting the viewport contents into a full page
view.render(painter)

# print the upper half of the viewport into the lower.
# half of the page.
viewport = view.viewport()->rect()
view.render(painter,
            QRectF(0, printer.height() / 2,
                   printer.width(), printer.height() / 2),
            viewport.adjusted(0, 0, 0, -viewport.height() / 2))

If source is a null rect, this function will use viewport() -> rect() to determine what to draw. If target is a null rect, the full dimensions of painter ‘s paint device (e.g., for a QPrinter , the page size) will be used.

The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in target .

See also

render()

PySide2.QtWidgets.QGraphicsView.renderHints()
Return type

RenderHints

This property holds the default render hints for the view.

These hints are used to initialize QPainter before each visible item is drawn. QPainter uses render hints to toggle rendering features such as antialiasing and smooth pixmap transformation.

TextAntialiasing is enabled by default.

Example:

scene = QGraphicsScene()
scene.addRect(QRectF(-10, -10, 20, 20))

view = QGraphicsView(scene)
view.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
view.show()
PySide2.QtWidgets.QGraphicsView.resetCachedContent()

Resets any cached content. Calling this function will clear QGraphicsView ‘s cache. If the current cache mode is CacheNone , this function does nothing.

This function is called automatically for you when the backgroundBrush or backgroundBrush properties change; you only need to call this function if you have reimplemented drawBackground() or drawBackground() to draw a custom background, and need to trigger a full redraw.

See also

cacheMode()

PySide2.QtWidgets.QGraphicsView.resetMatrix()

Note

This function is deprecated.

Use resetTransform() instead.

Resets the view transformation matrix to the identity matrix.

See also

resetTransform()

PySide2.QtWidgets.QGraphicsView.resetTransform()

Resets the view transformation to the identity matrix.

PySide2.QtWidgets.QGraphicsView.resizeAnchor()
Return type

ViewportAnchor

This property holds how the view should position the scene when the view is resized..

QGraphicsView uses this property to decide how to position the scene in the viewport when the viewport widget’s size changes. The default behavior, NoAnchor , leaves the scene’s position unchanged during a resize; the top-left corner of the view will appear to be anchored while resizing.

Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

PySide2.QtWidgets.QGraphicsView.rotate(angle)
Parameters

angle – float

Rotates the current view transformation angle degrees clockwise.

PySide2.QtWidgets.QGraphicsView.rubberBandChanged(viewportRect, fromScenePoint, toScenePoint)
Parameters
PySide2.QtWidgets.QGraphicsView.rubberBandRect()
Return type

PySide2.QtCore.QRect

This functions returns the current rubber band area (in viewport coordinates) if the user is currently doing an itemselection with rubber band. When the user is not using the rubber band this functions returns (a null) QRectF().

Notice that part of this QRect can be outise the visual viewport. It can e.g contain negative values.

PySide2.QtWidgets.QGraphicsView.rubberBandSelectionMode()
Return type

ItemSelectionMode

This property holds the behavior for selecting items with a rubber band selection rectangle..

This property defines how items are selected when using the RubberBandDrag drag mode.

The default value is IntersectsItemShape ; all items whose shape intersects with or is contained by the rubber band are selected.

PySide2.QtWidgets.QGraphicsView.scale(sx, sy)
Parameters
  • sx – float

  • sy – float

Scales the current view transformation by (sx , sy ).

PySide2.QtWidgets.QGraphicsView.scene()
Return type

PySide2.QtWidgets.QGraphicsScene

Returns a pointer to the scene that is currently visualized in the view. If no scene is currently visualized, None is returned.

See also

setScene()

PySide2.QtWidgets.QGraphicsView.sceneRect()
Return type

PySide2.QtCore.QRectF

This property holds the area of the scene visualized by this view..

The scene rectangle defines the extent of the scene, and in the view’s case, this means the area of the scene that you can navigate using the scroll bars.

If unset, or if a null QRectF is set, this property has the same value as sceneRect , and it changes with sceneRect . Otherwise, the view’s scene rect is unaffected by the scene.

Note that, although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX). When the scene is larger than the scroll bars’ values, you can choose to use translate() to navigate the scene instead.

By default, this property contains a rectangle at the origin with zero width and height.

See also

sceneRect

PySide2.QtWidgets.QGraphicsView.setAlignment(alignment)
Parameters

alignmentAlignment

This property holds the alignment of the scene in the view when the whole scene is visible..

If the whole scene is visible in the view, (i.e., there are no visible scroll bars,) the view’s alignment will decide where the scene will be rendered in the view. For example, if the alignment is AlignCenter , which is default, the scene will be centered in the view, and if the alignment is ( AlignLeft | AlignTop ), the scene will be rendered in the top-left corner of the view.

PySide2.QtWidgets.QGraphicsView.setBackgroundBrush(brush)
Parameters

brushPySide2.QtGui.QBrush

This property holds the background brush of the scene..

This property sets the background brush for the scene in this view. It is used to override the scene’s own background, and defines the behavior of drawBackground() . To provide custom background drawing for this view, you can reimplement drawBackground() instead.

By default, this property contains a brush with the NoBrush pattern.

PySide2.QtWidgets.QGraphicsView.setCacheMode(mode)
Parameters

modeCacheMode

This property holds which parts of the view are cached.

QGraphicsView can cache pre-rendered content in a QPixmap , which is then drawn onto the viewport. The purpose of such caching is to speed up the total rendering time for areas that are slow to render. Texture, gradient and alpha blended backgrounds, for example, can be notibly slow to render; especially with a transformed view. The CacheBackground flag enables caching of the view’s background. For example:

view = QGraphicsView()
view.setBackgroundBrush(QImage(":/images/backgroundtile.png"))
view.setCacheMode(QGraphicsView.CacheBackground)

The cache is invalidated every time the view is transformed. However, when scrolling, only partial invalidation is required.

By default, nothing is cached.

See also

resetCachedContent() QPixmapCache

PySide2.QtWidgets.QGraphicsView.setDragMode(mode)
Parameters

modeDragMode

This property holds the behavior for dragging the mouse over the scene while the left mouse button is pressed..

This property defines what should happen when the user clicks on the scene background and drags the mouse (e.g., scrolling the viewport contents using a pointing hand cursor, or selecting multiple items with a rubber band). The default value, NoDrag , does nothing.

This behavior only affects mouse clicks that are not handled by any item. You can define a custom behavior by creating a subclass of QGraphicsView and reimplementing mouseMoveEvent() .

PySide2.QtWidgets.QGraphicsView.setForegroundBrush(brush)
Parameters

brushPySide2.QtGui.QBrush

This property holds the foreground brush of the scene..

This property sets the foreground brush for the scene in this view. It is used to override the scene’s own foreground, and defines the behavior of drawForeground() . To provide custom foreground drawing for this view, you can reimplement drawForeground() instead.

By default, this property contains a brush with the NoBrush pattern.

PySide2.QtWidgets.QGraphicsView.setInteractive(allowed)
Parameters

allowed – bool

This property holds whether the view allows scene interaction..

If enabled, this view is set to allow scene interaction. Otherwise, this view will not allow interaction, and any mouse or key events are ignored (i.e., it will act as a read-only view).

By default, this property is true .

PySide2.QtWidgets.QGraphicsView.setMatrix(matrix[, combine=false])
Parameters

Note

This function is deprecated.

Use setTransform() instead.

Sets the view’s current transformation matrix to matrix .

If combine is true, then matrix is combined with the current matrix; otherwise, matrix replaces the current matrix. combine is false by default.

The transformation matrix tranforms the scene into view coordinates. Using the default transformation, provided by the identity matrix, one pixel in the view represents one unit in the scene (e.g., a 10x10 rectangular item is drawn using 10x10 pixels in the view). If a 2x2 scaling matrix is applied, the scene will be drawn in 1:2 (e.g., a 10x10 rectangular item is then drawn using 20x20 pixels in the view).

Example:

scene = QGraphicsScene()
scene.addText("GraphicsView rotated clockwise")

view = QGraphicsView(scene)
view.rotate(90) # the text is rendered with a 90 degree clockwise rotation
view.show()

To simplify interation with items using a transformed view, QGraphicsView provides mapTo … and mapFrom … functions that can translate between scene and view coordinates. For example, you can call mapToScene() to map a view coordinate to a floating point scene coordinate, or mapFromScene() to map from floating point scene coordinates to view coordinates.

PySide2.QtWidgets.QGraphicsView.setOptimizationFlag(flag[, enabled=true])
Parameters

Enables flag if enabled is true; otherwise disables flag .

PySide2.QtWidgets.QGraphicsView.setOptimizationFlags(flags)
Parameters

flagsOptimizationFlags

This property holds flags that can be used to tune QGraphicsView ‘s performance..

QGraphicsView uses clipping, extra bounding rect adjustments, and certain other aids to improve rendering quality and performance for the common case graphics scene. However, depending on the target platform, the scene, and the viewport in use, some of these operations can degrade performance.

The effect varies from flag to flag; see the OptimizationFlags documentation for details.

By default, no optimization flags are enabled.

PySide2.QtWidgets.QGraphicsView.setRenderHint(hint[, enabled=true])
Parameters

If enabled is true, the render hint hint is enabled; otherwise it is disabled.

See also

renderHints

PySide2.QtWidgets.QGraphicsView.setRenderHints(hints)
Parameters

hintsRenderHints

This property holds the default render hints for the view.

These hints are used to initialize QPainter before each visible item is drawn. QPainter uses render hints to toggle rendering features such as antialiasing and smooth pixmap transformation.

TextAntialiasing is enabled by default.

Example:

scene = QGraphicsScene()
scene.addRect(QRectF(-10, -10, 20, 20))

view = QGraphicsView(scene)
view.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
view.show()
PySide2.QtWidgets.QGraphicsView.setResizeAnchor(anchor)
Parameters

anchorViewportAnchor

This property holds how the view should position the scene when the view is resized..

QGraphicsView uses this property to decide how to position the scene in the viewport when the viewport widget’s size changes. The default behavior, NoAnchor , leaves the scene’s position unchanged during a resize; the top-left corner of the view will appear to be anchored while resizing.

Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

PySide2.QtWidgets.QGraphicsView.setRubberBandSelectionMode(mode)
Parameters

modeItemSelectionMode

This property holds the behavior for selecting items with a rubber band selection rectangle..

This property defines how items are selected when using the RubberBandDrag drag mode.

The default value is IntersectsItemShape ; all items whose shape intersects with or is contained by the rubber band are selected.

PySide2.QtWidgets.QGraphicsView.setScene(scene)
Parameters

scenePySide2.QtWidgets.QGraphicsScene

Sets the current scene to scene . If scene is already being viewed, this function does nothing.

When a scene is set on a view, the changed() signal is automatically connected to this view’s updateScene() slot, and the view’s scroll bars are adjusted to fit the size of the scene.

The view does not take ownership of scene .

See also

scene()

PySide2.QtWidgets.QGraphicsView.setSceneRect(rect)
Parameters

rectPySide2.QtCore.QRectF

This property holds the area of the scene visualized by this view..

The scene rectangle defines the extent of the scene, and in the view’s case, this means the area of the scene that you can navigate using the scroll bars.

If unset, or if a null QRectF is set, this property has the same value as sceneRect , and it changes with sceneRect . Otherwise, the view’s scene rect is unaffected by the scene.

Note that, although the scene supports a virtually unlimited size, the range of the scroll bars will never exceed the range of an integer (INT_MIN, INT_MAX). When the scene is larger than the scroll bars’ values, you can choose to use translate() to navigate the scene instead.

By default, this property contains a rectangle at the origin with zero width and height.

See also

sceneRect

PySide2.QtWidgets.QGraphicsView.setSceneRect(x, y, w, h)
Parameters
  • x – float

  • y – float

  • w – float

  • h – float

PySide2.QtWidgets.QGraphicsView.setTransform(matrix[, combine=false])
Parameters

Sets the view’s current transformation matrix to matrix .

If combine is true, then matrix is combined with the current matrix; otherwise, matrix replaces the current matrix. combine is false by default.

The transformation matrix tranforms the scene into view coordinates. Using the default transformation, provided by the identity matrix, one pixel in the view represents one unit in the scene (e.g., a 10x10 rectangular item is drawn using 10x10 pixels in the view). If a 2x2 scaling matrix is applied, the scene will be drawn in 1:2 (e.g., a 10x10 rectangular item is then drawn using 20x20 pixels in the view).

Example:

scene = QGraphicsScene()
scene.addText("GraphicsView rotated clockwise")

view = QGraphicsView(scene)
view.rotate(90) # the text is rendered with a 90 degree clockwise rotation
view.show()

To simplify interation with items using a transformed view, QGraphicsView provides mapTo … and mapFrom … functions that can translate between scene and view coordinates. For example, you can call mapToScene() to map a view coordiate to a floating point scene coordinate, or mapFromScene() to map from floating point scene coordinates to view coordinates.

PySide2.QtWidgets.QGraphicsView.setTransformationAnchor(anchor)
Parameters

anchorViewportAnchor

This property holds how the view should position the scene during transformations..

QGraphicsView uses this property to decide how to position the scene in the viewport when the transformation matrix changes, and the coordinate system of the view is transformed. The default behavior, AnchorViewCenter , ensures that the scene point at the center of the view remains unchanged during transformations (e.g., when rotating, the scene will appear to rotate around the center of the view).

Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

PySide2.QtWidgets.QGraphicsView.setViewportUpdateMode(mode)
Parameters

modeViewportUpdateMode

This property holds how the viewport should update its contents..

QGraphicsView uses this property to decide how to update areas of the scene that have been reexposed or changed. Usually you do not need to modify this property, but there are some cases where doing so can improve rendering performance. See the ViewportUpdateMode documentation for specific details.

The default value is MinimalViewportUpdate , where QGraphicsView will update as small an area of the viewport as possible when the contents change.

See also

ViewportUpdateMode cacheMode

PySide2.QtWidgets.QGraphicsView.shear(sh, sv)
Parameters
  • sh – float

  • sv – float

Shears the current view transformation by (sh , sv ).

PySide2.QtWidgets.QGraphicsView.transform()
Return type

PySide2.QtGui.QTransform

Returns the current transformation matrix for the view. If no current transformation is set, the identity matrix is returned.

PySide2.QtWidgets.QGraphicsView.transformationAnchor()
Return type

ViewportAnchor

This property holds how the view should position the scene during transformations..

QGraphicsView uses this property to decide how to position the scene in the viewport when the transformation matrix changes, and the coordinate system of the view is transformed. The default behavior, AnchorViewCenter , ensures that the scene point at the center of the view remains unchanged during transformations (e.g., when rotating, the scene will appear to rotate around the center of the view).

Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

PySide2.QtWidgets.QGraphicsView.translate(dx, dy)
Parameters
  • dx – float

  • dy – float

Translates the current view transformation by (dx , dy ).

PySide2.QtWidgets.QGraphicsView.updateScene(rects)
Parameters

rects

Schedules an update of the scene rectangles rects .

See also

changed()

PySide2.QtWidgets.QGraphicsView.updateSceneRect(rect)
Parameters

rectPySide2.QtCore.QRectF

Notifies QGraphicsView that the scene’s scene rect has changed. rect is the new scene rect. If the view already has an explicitly set scene rect, this function does nothing.

PySide2.QtWidgets.QGraphicsView.viewportTransform()
Return type

PySide2.QtGui.QTransform

Returns a matrix that maps scene coordinates to viewport coordinates.

PySide2.QtWidgets.QGraphicsView.viewportUpdateMode()
Return type

ViewportUpdateMode

This property holds how the viewport should update its contents..

QGraphicsView uses this property to decide how to update areas of the scene that have been reexposed or changed. Usually you do not need to modify this property, but there are some cases where doing so can improve rendering performance. See the ViewportUpdateMode documentation for specific details.

The default value is MinimalViewportUpdate , where QGraphicsView will update as small an area of the viewport as possible when the contents change.

See also

ViewportUpdateMode cacheMode