Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QGraphicsView Class Reference

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

#include <qgraphicsview.h>

+ Inheritance diagram for QGraphicsView:
+ Collaboration diagram for QGraphicsView:

Public Types

enum  ViewportAnchor { NoAnchor , AnchorViewCenter , AnchorUnderMouse }
 This enums describe the possible anchors that QGraphicsView can use when the user resizes the view or when the view is transformed. More...
 
enum  CacheModeFlag { CacheNone = 0x0 , CacheBackground = 0x1 }
 This enum describes the flags that you can set for a QGraphicsView's cache mode. More...
 
enum  DragMode { NoDrag , ScrollHandDrag , RubberBandDrag }
 This enum describes the default action for the view when pressing and dragging the mouse over the viewport. More...
 
enum  ViewportUpdateMode {
  FullViewportUpdate , MinimalViewportUpdate , SmartViewportUpdate , NoViewportUpdate ,
  BoundingRectViewportUpdate
}
 
enum  OptimizationFlag { DontSavePainterState = 0x1 , DontAdjustForAntialiasing = 0x2 , IndirectPainting = 0x4 }
 

Public Slots

void updateScene (const QList< QRectF > &rects)
 Schedules an update of the scene rectangles rects.
 
void invalidateScene (const QRectF &rect=QRectF(), QGraphicsScene::SceneLayers layers=QGraphicsScene::AllLayers)
 Invalidates and schedules a redraw of layers inside rect.
 
void updateSceneRect (const QRectF &rect)
 Notifies QGraphicsView that the scene's scene rect has changed.
 

Public Member Functions

 QGraphicsView (QWidget *parent=nullptr)
 Constructs a QGraphicsView.
 
 QGraphicsView (QGraphicsScene *scene, QWidget *parent=nullptr)
 Constructs a QGraphicsView and sets the visualized scene to scene.
 
 ~QGraphicsView ()
 Destructs the QGraphicsView object.
 
QSize sizeHint () const override
 \reimp
 
QPainter::RenderHints renderHints () const
 
void setRenderHint (QPainter::RenderHint hint, bool enabled=true)
 If enabled is true, the render hint hint is enabled; otherwise it is disabled.
 
void setRenderHints (QPainter::RenderHints hints)
 
Qt::Alignment alignment () const
 
void setAlignment (Qt::Alignment alignment)
 
ViewportAnchor transformationAnchor () const
 
void setTransformationAnchor (ViewportAnchor anchor)
 
ViewportAnchor resizeAnchor () const
 
void setResizeAnchor (ViewportAnchor anchor)
 
ViewportUpdateMode viewportUpdateMode () const
 
void setViewportUpdateMode (ViewportUpdateMode mode)
 
OptimizationFlags optimizationFlags () const
 
void setOptimizationFlag (OptimizationFlag flag, bool enabled=true)
 Enables flag if enabled is true; otherwise disables flag.
 
void setOptimizationFlags (OptimizationFlags flags)
 
DragMode dragMode () const
 
void setDragMode (DragMode mode)
 
CacheMode cacheMode () const
 
void setCacheMode (CacheMode mode)
 
void resetCachedContent ()
 Resets any cached content.
 
bool isInteractive () const
 
void setInteractive (bool allowed)
 
QGraphicsScenescene () const
 Returns a pointer to the scene that is currently visualized in the view.
 
void setScene (QGraphicsScene *scene)
 Sets the current scene to scene.
 
QRectF sceneRect () const
 
void setSceneRect (const QRectF &rect)
 
void setSceneRect (qreal x, qreal y, qreal w, qreal h)
 
QTransform transform () const
 Returns the current transformation matrix for the view.
 
QTransform viewportTransform () const
 Returns a matrix that maps scene coordinates to viewport coordinates.
 
bool isTransformed () const
 
void setTransform (const QTransform &matrix, bool combine=false)
 Sets the view's current transformation matrix to matrix.
 
void resetTransform ()
 Resets the view transformation to the identity matrix.
 
void rotate (qreal angle)
 Rotates the current view transformation angle degrees clockwise.
 
void scale (qreal sx, qreal sy)
 Scales the current view transformation by (sx, sy).
 
void shear (qreal sh, qreal sv)
 Shears the current view transformation by (sh, sv).
 
void translate (qreal dx, qreal dy)
 Translates the current view transformation by (dx, dy).
 
void centerOn (const QPointF &pos)
 Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view.
 
void centerOn (qreal x, qreal y)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This function is provided for convenience.
 
void centerOn (const QGraphicsItem *item)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Scrolls the contents of the viewport to ensure that item is centered in the view.
 
void ensureVisible (const QRectF &rect, int xmargin=50, int ymargin=50)
 Scrolls the contents of the viewport so that the scene rectangle rect is visible, with margins specified in pixels by xmargin and ymargin.
 
void ensureVisible (qreal x, qreal y, qreal w, qreal h, int xmargin=50, int ymargin=50)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This function is provided for convenience.
 
void ensureVisible (const QGraphicsItem *item, int xmargin=50, int ymargin=50)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Scrolls the contents of the viewport so that the center of item item is visible, with margins specified in pixels by xmargin and ymargin.
 
void fitInView (const QRectF &rect, Qt::AspectRatioMode aspectRadioMode=Qt::IgnoreAspectRatio)
 Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.
 
void fitInView (qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRadioMode=Qt::IgnoreAspectRatio)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This convenience function is equivalent to calling fitInView(QRectF(x, y, w, h), aspectRatioMode).
 
void fitInView (const QGraphicsItem *item, Qt::AspectRatioMode aspectRadioMode=Qt::IgnoreAspectRatio)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode.
 
void render (QPainter *painter, const QRectF &target=QRectF(), const QRect &source=QRect(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
 Renders the source rect, which is in view coordinates, from the scene into target, which is in paint device coordinates, using painter.
 
QList< QGraphicsItem * > items () const
 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).
 
QList< QGraphicsItem * > items (const QPoint &pos) const
 Returns a list of all the items at the position pos in the view.
 
QList< QGraphicsItem * > items (int x, int y) const
 This function is provided for convenience.
 
QList< QGraphicsItem * > items (const QRect &rect, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with rect.
 
QList< QGraphicsItem * > items (int x, int y, int w, int h, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
 
QList< QGraphicsItem * > items (const QPolygon &polygon, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with polygon.
 
QList< QGraphicsItem * > items (const QPainterPath &path, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with path.
 
QGraphicsItemitemAt (const QPoint &pos) const
 Returns the item at position pos, which is in viewport coordinates.
 
QGraphicsItemitemAt (int x, int y) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QPointF mapToScene (const QPoint &point) const
 Returns the viewport coordinate point mapped to scene coordinates.
 
QPolygonF mapToScene (const QRect &rect) const
 Returns the viewport rectangle rect mapped to a scene coordinate polygon.
 
QPolygonF mapToScene (const QPolygon &polygon) const
 Returns the viewport polygon polygon mapped to a scene coordinate polygon.
 
QPainterPath mapToScene (const QPainterPath &path) const
 Returns the viewport painter path path mapped to a scene coordinate painter path.
 
QPoint mapFromScene (const QPointF &point) const
 Returns the scene coordinate point to viewport coordinates.
 
QPolygon mapFromScene (const QRectF &rect) const
 Returns the scene rectangle rect to a viewport coordinate polygon.
 
QPolygon mapFromScene (const QPolygonF &polygon) const
 Returns the scene coordinate polygon polygon to a viewport coordinate polygon.
 
QPainterPath mapFromScene (const QPainterPath &path) const
 Returns the scene coordinate painter path path to a viewport coordinate painter path.
 
QPointF mapToScene (int x, int y) const
 This function is provided for convenience.
 
QPolygonF mapToScene (int x, int y, int w, int h) const
 This function is provided for convenience.
 
QPoint mapFromScene (qreal x, qreal y) const
 This function is provided for convenience.
 
QPolygon mapFromScene (qreal x, qreal y, qreal w, qreal h) const
 This function is provided for convenience.
 
QVariant inputMethodQuery (Qt::InputMethodQuery query) const override
 \reimp
 
QBrush backgroundBrush () const
 
void setBackgroundBrush (const QBrush &brush)
 
QBrush foregroundBrush () const
 
void setForegroundBrush (const QBrush &brush)
 

Protected Slots

void setupViewport (QWidget *widget) override
 This slot is called by QAbstractScrollArea after setViewport() has been called.
 

Protected Member Functions

 QGraphicsView (QGraphicsViewPrivate &, QWidget *parent=nullptr)
 
bool event (QEvent *event) override
 \reimp
 
bool viewportEvent (QEvent *event) override
 \reimp
 
void contextMenuEvent (QContextMenuEvent *event) override
 \reimp
 
void focusInEvent (QFocusEvent *event) override
 \reimp
 
bool focusNextPrevChild (bool next) override
 \reimp
 
void focusOutEvent (QFocusEvent *event) override
 \reimp
 
void keyPressEvent (QKeyEvent *event) override
 \reimp
 
void keyReleaseEvent (QKeyEvent *event) override
 \reimp
 
void mouseDoubleClickEvent (QMouseEvent *event) override
 \reimp
 
void mousePressEvent (QMouseEvent *event) override
 \reimp
 
void mouseMoveEvent (QMouseEvent *event) override
 \reimp
 
void mouseReleaseEvent (QMouseEvent *event) override
 \reimp
 
void paintEvent (QPaintEvent *event) override
 \reimp
 
void resizeEvent (QResizeEvent *event) override
 \reimp
 
void scrollContentsBy (int dx, int dy) override
 \reimp
 
void showEvent (QShowEvent *event) override
 \reimp
 
void inputMethodEvent (QInputMethodEvent *event) override
 \reimp
 
virtual void drawBackground (QPainter *painter, const QRectF &rect)
 Draws the background of the scene using painter, before any items and the foreground are drawn.
 
virtual void drawForeground (QPainter *painter, const QRectF &rect)
 Draws the foreground of the scene using painter, after the background and all items are drawn.
 
virtual void drawItems (QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[])
 

Properties

QBrush backgroundBrush
 the background brush of the scene.
 
QBrush foregroundBrush
 the foreground brush of the scene.
 
bool interactive
 whether the view allows scene interaction.
 
QRectF sceneRect
 the area of the scene visualized by this view.
 
Qt::Alignment alignment
 the alignment of the scene in the view when the whole scene is visible.
 
QPainter::RenderHints renderHints
 the default render hints for the view
 
DragMode dragMode
 the behavior for dragging the mouse over the scene while the left mouse button is pressed.
 
CacheMode cacheMode
 which parts of the view are cached
 
ViewportAnchor transformationAnchor
 how the view should position the scene during transformations.
 
ViewportAnchor resizeAnchor
 how the view should position the scene when the view is resized.
 
ViewportUpdateMode viewportUpdateMode
 how the viewport should update its contents.
 
OptimizationFlags optimizationFlags
 flags that can be used to tune QGraphicsView's performance.
 

Friends

class QGraphicsScene
 
class QGraphicsScenePrivate
 
class QGraphicsItemPrivate
 

Detailed Description

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

Since
4.2

\inmodule QtWidgets

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 \l{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.addText("Hello, world!");
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 QGraphicsScene::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 (setAlignment()), 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. Similarly, 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.

When using a QOpenGLWidget as a viewport, stereoscopic rendering is supported. This is done using the same pattern as QOpenGLWidget::paintGL. To enable it, enable the QSurfaceFormat::StereoBuffers flag. Because of how the flag is handled internally, set QSurfaceFormat::StereoBuffers flag globally before the window is created using QSurfaceFormat::setDefaultFormat(). If the flag is enabled and there is hardware support for stereoscopic rendering, then drawBackground() and drawForeground() will be triggered twice each frame. Call QOpenGLWidget::currentTargetBuffer() to query which buffer is currently being drawn to.

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.
See also
QGraphicsScene, QGraphicsItem, QGraphicsSceneEvent

Definition at line 23 of file qgraphicsview.h.

Member Enumeration Documentation

◆ CacheModeFlag

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

\value CacheNone All painting is done directly onto the viewport.

\value 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
Enumerator
CacheNone 
CacheBackground 

Definition at line 55 of file qgraphicsview.h.

◆ DragMode

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

\value NoDrag Nothing happens; the mouse event is ignored.

\value ScrollHandDrag The cursor changes into a pointing hand, and dragging the mouse around will scroll the scrolbars. This mode works both in \l{QGraphicsView::interactive}{interactive} and non-interactive mode.

\value 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.

See also
dragMode, QGraphicsScene::setSelectionArea()
Enumerator
NoDrag 
ScrollHandDrag 
RubberBandDrag 

Definition at line 61 of file qgraphicsview.h.

◆ OptimizationFlag

Since
4.3

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.

\value DontSavePainterState When rendering, QGraphicsView protects the painter state (see QPainter::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 QPainter::setPen() or QPainter::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.

\value DontAdjustForAntialiasing Disables QGraphicsView's antialiasing auto-adjustment of exposed areas. Items that render antialiased lines on the boundaries of their QGraphicsItem::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.

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

Enumerator
DontSavePainterState 
DontAdjustForAntialiasing 
IndirectPainting 

Definition at line 77 of file qgraphicsview.h.

◆ ViewportAnchor

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

\value NoAnchor No anchor, i.e. the view leaves the scene's position unchanged. \value AnchorViewCenter The scene point at the center of the view is used as the anchor. \value AnchorUnderMouse The point under the mouse is used as the anchor.

See also
resizeAnchor, transformationAnchor
Enumerator
NoAnchor 
AnchorViewCenter 
AnchorUnderMouse 

Definition at line 48 of file qgraphicsview.h.

◆ ViewportUpdateMode

Since
4.3

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

\value 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.

\value 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.

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

\value 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.

\value 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.

See also
viewportUpdateMode
Enumerator
FullViewportUpdate 
MinimalViewportUpdate 
SmartViewportUpdate 
NoViewportUpdate 
BoundingRectViewportUpdate 

Definition at line 68 of file qgraphicsview.h.

Constructor & Destructor Documentation

◆ QGraphicsView() [1/3]

QGraphicsView::QGraphicsView ( QWidget * parent = nullptr)

Constructs a QGraphicsView.

parent is passed to QWidget's constructor.

Definition at line 1207 of file qgraphicsview.cpp.

References QPalette::Base, setAttribute(), setBackgroundRole(), viewport(), and Qt::WA_InputMethodEnabled.

+ Here is the call graph for this function:

◆ QGraphicsView() [2/3]

QGraphicsView::QGraphicsView ( QGraphicsScene * scene,
QWidget * parent = nullptr )

Constructs a QGraphicsView and sets the visualized scene to scene.

parent is passed to QWidget's constructor.

Definition at line 1222 of file qgraphicsview.cpp.

References QPalette::Base, scene(), setAttribute(), setBackgroundRole(), setScene(), viewport(), and Qt::WA_InputMethodEnabled.

+ Here is the call graph for this function:

◆ ~QGraphicsView()

QGraphicsView::~QGraphicsView ( )

Destructs the QGraphicsView object.

Definition at line 1251 of file qgraphicsview.cpp.

References d.

◆ QGraphicsView() [3/3]

QGraphicsView::QGraphicsView ( QGraphicsViewPrivate & dd,
QWidget * parent = nullptr )
protected

Definition at line 1237 of file qgraphicsview.cpp.

References QPalette::Base, setAttribute(), setBackgroundRole(), viewport(), and Qt::WA_InputMethodEnabled.

+ Here is the call graph for this function:

Member Function Documentation

◆ alignment()

Qt::Alignment QGraphicsView::alignment ( ) const

Definition at line 1329 of file qgraphicsview.cpp.

References d.

◆ backgroundBrush()

QBrush QGraphicsView::backgroundBrush ( ) const

Definition at line 2582 of file qgraphicsview.cpp.

References d.

◆ cacheMode()

QGraphicsView::CacheMode QGraphicsView::cacheMode ( ) const

Definition at line 1597 of file qgraphicsview.cpp.

References d.

◆ centerOn() [1/3]

void QGraphicsView::centerOn ( const QGraphicsItem * item)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Scrolls the contents of the viewport to ensure that item is centered in the view.

See also
ensureVisible()

Definition at line 1921 of file qgraphicsview.cpp.

References QRectF::center(), centerOn(), item, and QGraphicsItem::sceneBoundingRect().

+ Here is the call graph for this function:

◆ centerOn() [2/3]

void QGraphicsView::centerOn ( const QPointF & pos)

Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view.

Because pos is a floating point coordinate, and the scroll bars operate on integer coordinates, the centering is only an approximation.

Note
If the item is close to or outside the border, it will be visible in the view, but not centered.
See also
ensureVisible()

Definition at line 1881 of file qgraphicsview.cpp.

References d, pos, qRound(), and viewport().

Referenced by centerOn(), centerOn(), fitInView(), and QPrintPreviewWidgetPrivate::setCurrentPage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ centerOn() [3/3]

void QGraphicsView::centerOn ( qreal x,
qreal y )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This function is provided for convenience.

It's equivalent to calling centerOn(QPointF(x, y)).

Definition at line 258 of file qgraphicsview.h.

References centerOn().

+ Here is the call graph for this function:

◆ contextMenuEvent()

void QGraphicsView::contextMenuEvent ( QContextMenuEvent * event)
overrideprotected

\reimp

Definition at line 2943 of file qgraphicsview.cpp.

References d, QEvent::GraphicsSceneContextMenu, mapToScene(), QCoreApplication::sendEvent(), and viewport().

+ Here is the call graph for this function:

◆ dragMode()

QGraphicsView::DragMode QGraphicsView::dragMode ( ) const

Definition at line 1492 of file qgraphicsview.cpp.

References d.

◆ drawBackground()

void QGraphicsView::drawBackground ( QPainter * painter,
const QRectF & rect )
protectedvirtual

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 drawBackground() function is called instead.

See also
drawForeground(), QGraphicsScene::drawBackground()

Definition at line 3712 of file qgraphicsview.cpp.

References QPainter::Antialiasing, d, QPainter::fillRect(), Qt::NoBrush, painter, rect, QPainter::setRenderHints(), and QPainter::testRenderHint().

Referenced by paintEvent(), and render().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ drawForeground()

void QGraphicsView::drawForeground ( QPainter * painter,
const QRectF & rect )
protectedvirtual

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 drawForeground() function is called instead.

See also
drawBackground(), QGraphicsScene::drawForeground()

Definition at line 3745 of file qgraphicsview.cpp.

References d, QPainter::fillRect(), Qt::NoBrush, painter, and rect.

Referenced by paintEvent(), and render().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ drawItems()

void QGraphicsView::drawItems ( QPainter * painter,
int numItems,
QGraphicsItem * items[],
const QStyleOptionGraphicsItem options[] )
protectedvirtual
Deprecated

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 drawItems() function.

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

See also
drawForeground(), drawBackground(), QGraphicsScene::drawItems()

Definition at line 3773 of file qgraphicsview.cpp.

References d, QPainter::device(), items(), painter, viewport(), and widget.

Referenced by paintEvent(), and render().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ensureVisible() [1/3]

void QGraphicsView::ensureVisible ( const QGraphicsItem * item,
int xmargin = 50,
int ymargin = 50 )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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()

Definition at line 1988 of file qgraphicsview.cpp.

References ensureVisible(), item, and QGraphicsItem::sceneBoundingRect().

+ Here is the call graph for this function:

◆ ensureVisible() [2/3]

void QGraphicsView::ensureVisible ( const QRectF & rect,
int xmargin = 50,
int ymargin = 50 )

Scrolls the contents of the viewport so that the scene rectangle rect is visible, with margins specified in pixels by xmargin and ymargin.

If the specified rect cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.

See also
centerOn()

Definition at line 1935 of file qgraphicsview.cpp.

References d, rect, and viewport().

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), ensureVisible(), and ensureVisible().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ensureVisible() [3/3]

void QGraphicsView::ensureVisible ( qreal x,
qreal y,
qreal w,
qreal h,
int xmargin = 50,
int ymargin = 50 )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This function is provided for convenience.

It's equivalent to calling ensureVisible(QRectF(x, y, w, h), xmargin, ymargin).

Definition at line 260 of file qgraphicsview.h.

References ensureVisible().

+ Here is the call graph for this function:

◆ event()

bool QGraphicsView::event ( QEvent * event)
overrideprotected

\reimp

Definition at line 2772 of file qgraphicsview.cpp.

References d, QKeyEvent::key(), Qt::Key_Backtab, Qt::Key_Tab, QEvent::KeyPress, QCoreApplication::sendEvent(), and QEvent::ShortcutOverride.

+ Here is the call graph for this function:

◆ fitInView() [1/3]

void QGraphicsView::fitInView ( const QGraphicsItem * item,
Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Ensures that item fits tightly inside the view, scaling the view according to aspectRatioMode.

See also
ensureVisible(), centerOn()

Definition at line 2077 of file qgraphicsview.cpp.

References QGraphicsItem::clipPath(), QGraphicsItem::d_ptr, QTransform::dx(), QTransform::dy(), fitInView(), QGraphicsItemPrivate::hasTranslateOnlySceneTransform(), QGraphicsItem::isClipped(), item, QTransform::map(), QGraphicsItemPrivate::sceneTransform, and QGraphicsItem::shape().

+ Here is the call graph for this function:

◆ fitInView() [2/3]

void QGraphicsView::fitInView ( const QRectF & rect,
Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )

Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.

rect must be inside the scene rect; otherwise, fitInView() cannot guarantee that the whole rect is visible.

This function keeps the view's rotation, translation, or shear. The view is scaled according to aspectRatioMode. rect will be centered in the view if it does not fit tightly.

It's common to call fitInView() from inside a reimplementation of resizeEvent(), to ensure that the whole scene, or parts of the scene, scales automatically to fit the new size of the viewport as the view is resized. Note though, that calling fitInView() from inside resizeEvent() can lead to unwanted resize recursion, if the new transformation toggles the automatic state of the scrollbars. You can toggle the scrollbar policies to always on or always off to prevent this (see horizontalScrollBarPolicy() and verticalScrollBarPolicy()).

If rect is empty, or if the viewport is too small, this function will do nothing.

See also
setTransform(), ensureVisible(), centerOn()

Definition at line 2017 of file qgraphicsview.cpp.

References QRectF::adjusted(), centerOn(), d, QRectF::height(), Qt::IgnoreAspectRatio, QRectF::isEmpty(), Qt::KeepAspectRatio, Qt::KeepAspectRatioByExpanding, qMax(), qMin(), rect, sceneRect, viewport(), and QRectF::width().

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), fitInView(), and fitInView().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fitInView() [3/3]

void QGraphicsView::fitInView ( qreal x,
qreal y,
qreal w,
qreal h,
Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.This convenience function is equivalent to calling fitInView(QRectF(x, y, w, h), aspectRatioMode).

See also
ensureVisible(), centerOn()

Definition at line 262 of file qgraphicsview.h.

References fitInView().

+ Here is the call graph for this function:

◆ focusInEvent()

void QGraphicsView::focusInEvent ( QFocusEvent * event)
overrideprotected

\reimp

Definition at line 3088 of file qgraphicsview.cpp.

References d, and QCoreApplication::sendEvent().

+ Here is the call graph for this function:

◆ focusNextPrevChild()

bool QGraphicsView::focusNextPrevChild ( bool next)
overrideprotected

\reimp

Definition at line 3103 of file qgraphicsview.cpp.

References next.

◆ focusOutEvent()

void QGraphicsView::focusOutEvent ( QFocusEvent * event)
overrideprotected

\reimp

Definition at line 3111 of file qgraphicsview.cpp.

References d, and QCoreApplication::sendEvent().

+ Here is the call graph for this function:

◆ foregroundBrush()

QBrush QGraphicsView::foregroundBrush ( ) const

Definition at line 2612 of file qgraphicsview.cpp.

References d.

◆ inputMethodEvent()

void QGraphicsView::inputMethodEvent ( QInputMethodEvent * event)
overrideprotected

\reimp

Definition at line 3688 of file qgraphicsview.cpp.

References d, and QCoreApplication::sendEvent().

+ Here is the call graph for this function:

◆ inputMethodQuery()

QVariant QGraphicsView::inputMethodQuery ( Qt::InputMethodQuery query) const
override

\reimp

Definition at line 2551 of file qgraphicsview.cpp.

References d, and mapFromScene().

+ Here is the call graph for this function:

◆ invalidateScene

void QGraphicsView::invalidateScene ( const QRectF & rect = QRectF(),
QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers )
slot

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 QGraphicsView::CacheBackground). This function is equivalent to calling update() if any layer but QGraphicsScene::BackgroundLayer is passed.

See also
QGraphicsScene::invalidate(), update()

Definition at line 1659 of file qgraphicsview.cpp.

References QGraphicsScene::BackgroundLayer, d, mapFromScene(), rect, and viewport().

+ Here is the call graph for this function:

◆ isInteractive()

bool QGraphicsView::isInteractive ( ) const

Definition at line 1684 of file qgraphicsview.cpp.

References d.

◆ isTransformed()

bool QGraphicsView::isTransformed ( ) const
Since
4.6

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

See also
setTransform(), horizontalScrollBar(), verticalScrollBar()

Definition at line 3816 of file qgraphicsview.cpp.

References d.

Referenced by paintEvent().

+ Here is the caller graph for this function:

◆ itemAt() [1/2]

QGraphicsItem * QGraphicsView::itemAt ( const QPoint & pos) const

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:

void CustomView::mousePressEvent(QMouseEvent *event)
{
if (QGraphicsItem *item = itemAt(event->pos())) {
qDebug() << "You clicked on item" << item;
} else {
qDebug("You didn't click on an item.");
}
}
See also
items(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2339 of file qgraphicsview.cpp.

References d, items(), nullptr, and pos.

Referenced by itemAt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ itemAt() [2/2]

QGraphicsItem * QGraphicsView::itemAt ( int x,
int y ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

Definition at line 268 of file qgraphicsview.h.

References itemAt().

+ Here is the call graph for this function:

◆ items() [1/7]

QList< QGraphicsItem * > QGraphicsView::items ( ) const

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
QGraphicsScene::items(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2202 of file qgraphicsview.cpp.

References d.

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), QPrintPreviewWidgetPrivate::calcCurrentPage(), drawItems(), itemAt(), items(), and items().

+ Here is the caller graph for this function:

◆ items() [2/7]

QList< QGraphicsItem * > QGraphicsView::items ( const QPainterPath & path,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with path.

path is in viewport coordinates.

The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by path are returned.

See also
itemAt(), items(), mapToScene(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2320 of file qgraphicsview.cpp.

References d, Qt::DescendingOrder, mapToScene(), and viewportTransform().

+ Here is the call graph for this function:

◆ items() [3/7]

QList< QGraphicsItem * > QGraphicsView::items ( const QPoint & pos) const

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 QMouseEvent::pos().

void CustomView::mousePressEvent(QMouseEvent *event)
{
qDebug() << "There are" << items(event->pos()).size()
<< "items at position" << mapToScene(event->pos());
}
See also
QGraphicsScene::items(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2224 of file qgraphicsview.cpp.

References d, Qt::DescendingOrder, Qt::IntersectsItemShape, QTransform::inverted(), mapToScene(), pos, QTransform::TxScale, and viewportTransform().

+ Here is the call graph for this function:

◆ items() [4/7]

QList< QGraphicsItem * > QGraphicsView::items ( const QPolygon & polygon,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with polygon.

polygon is in viewport coordinates.

The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by polygon are returned.

The items are sorted by descending stacking order (i.e., the first item in the returned list is the uppermost item).

See also
itemAt(), items(), mapToScene(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2300 of file qgraphicsview.cpp.

References d, Qt::DescendingOrder, mapToScene(), and viewportTransform().

+ Here is the call graph for this function:

◆ items() [5/7]

QList< QGraphicsItem * > QGraphicsView::items ( const QRect & rect,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list of all the items that, depending on mode, are either contained by or intersect with rect.

rect is in viewport coordinates.

The default value for mode is Qt::IntersectsItemShape; all items whose exact shape intersects with or is contained by rect are returned.

The items are sorted in descending stacking order (i.e., the first item in the returned list is the uppermost item).

See also
itemAt(), items(), mapToScene(), {QGraphicsItem::Sorting}{Sorting}

Definition at line 2269 of file qgraphicsview.cpp.

References d, Qt::DescendingOrder, mapToScene(), rect, and viewportTransform().

+ Here is the call graph for this function:

◆ items() [6/7]

QList< QGraphicsItem * > QGraphicsView::items ( int x,
int y ) const
inline

This function is provided for convenience.

It's equivalent to calling items(QPoint(x, y)).

Definition at line 264 of file qgraphicsview.h.

References items().

+ Here is the call graph for this function:

◆ items() [7/7]

QList< QGraphicsItem * > QGraphicsView::items ( int x,
int y,
int w,
int h,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
inline
Since
4.3

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

Definition at line 266 of file qgraphicsview.h.

References items().

+ Here is the call graph for this function:

◆ keyPressEvent()

void QGraphicsView::keyPressEvent ( QKeyEvent * event)
overrideprotected

\reimp

Definition at line 3122 of file qgraphicsview.cpp.

References d, and QCoreApplication::sendEvent().

+ Here is the call graph for this function:

◆ keyReleaseEvent()

void QGraphicsView::keyReleaseEvent ( QKeyEvent * event)
overrideprotected

\reimp

Definition at line 3137 of file qgraphicsview.cpp.

References d, and QCoreApplication::sendEvent().

+ Here is the call graph for this function:

◆ mapFromScene() [1/6]

QPainterPath QGraphicsView::mapFromScene ( const QPainterPath & path) const

Returns the scene coordinate painter path path to a viewport coordinate painter path.

See also
mapToScene()

Definition at line 2540 of file qgraphicsview.cpp.

References d, and QTransform::fromTranslate().

+ Here is the call graph for this function:

◆ mapFromScene() [2/6]

QPoint QGraphicsView::mapFromScene ( const QPointF & point) const

Returns the scene coordinate point to viewport coordinates.

See also
mapToScene()

Definition at line 2457 of file qgraphicsview.cpp.

References d, and QPointF::rx().

Referenced by QPrintPreviewWidgetPrivate::calcCurrentPage(), inputMethodQuery(), invalidateScene(), mapFromScene(), mapFromScene(), and mapFromScene().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mapFromScene() [3/6]

QPolygon QGraphicsView::mapFromScene ( const QPolygonF & polygon) const

Returns the scene coordinate polygon polygon to a viewport coordinate polygon.

See also
mapToScene()

Definition at line 2525 of file qgraphicsview.cpp.

References mapFromScene(), and QList< T >::reserve().

+ Here is the call graph for this function:

◆ mapFromScene() [4/6]

QPolygon QGraphicsView::mapFromScene ( const QRectF & rect) const

Returns the scene rectangle rect to a viewport coordinate polygon.

See also
mapToScene()

Definition at line 2479 of file qgraphicsview.cpp.

References d, rect, QPointF::toPoint(), and tr.

+ Here is the call graph for this function:

◆ mapFromScene() [5/6]

QPoint QGraphicsView::mapFromScene ( qreal x,
qreal y ) const
inline

This function is provided for convenience.

It's equivalent to calling mapFromScene(QPointF(x, y)).

Definition at line 274 of file qgraphicsview.h.

References mapFromScene().

+ Here is the call graph for this function:

◆ mapFromScene() [6/6]

QPolygon QGraphicsView::mapFromScene ( qreal x,
qreal y,
qreal w,
qreal h ) const
inline

This function is provided for convenience.

It's equivalent to calling mapFromScene(QRectF(x, y, w, h)).

Definition at line 276 of file qgraphicsview.h.

References mapFromScene().

+ Here is the call graph for this function:

◆ mapToScene() [1/6]

QPainterPath QGraphicsView::mapToScene ( const QPainterPath & path) const

Returns the viewport painter path path mapped to a scene coordinate painter path.

See also
mapFromScene()

Definition at line 2444 of file qgraphicsview.cpp.

References d, and QTransform::fromTranslate().

+ Here is the call graph for this function:

◆ mapToScene() [2/6]

QPointF QGraphicsView::mapToScene ( const QPoint & point) const

Returns the viewport coordinate point mapped to scene coordinates.

Note: It can be useful to map the whole rectangle covered by the pixel at point instead of the point itself. To do this, you can call mapToScene(QRect(point, QSize(2, 2))).

See also
mapFromScene()

Definition at line 2365 of file qgraphicsview.cpp.

References d, and QPoint::rx().

Referenced by contextMenuEvent(), items(), items(), items(), items(), mapToScene(), mapToScene(), mapToScene(), mouseDoubleClickEvent(), mousePressEvent(), mouseReleaseEvent(), paintEvent(), render(), and viewportEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mapToScene() [3/6]

QPolygonF QGraphicsView::mapToScene ( const QPolygon & polygon) const

Returns the viewport polygon polygon mapped to a scene coordinate polygon.

See also
mapFromScene()

Definition at line 2429 of file qgraphicsview.cpp.

References mapToScene(), and QList< T >::reserve().

+ Here is the call graph for this function:

◆ mapToScene() [4/6]

QPolygonF QGraphicsView::mapToScene ( const QRect & rect) const

Returns the viewport rectangle rect mapped to a scene coordinate polygon.

See also
mapFromScene()

Definition at line 2387 of file qgraphicsview.cpp.

References QRect::adjusted(), d, QTransform::inverted(), QTransform::map(), rect, and tr.

+ Here is the call graph for this function:

◆ mapToScene() [5/6]

QPointF QGraphicsView::mapToScene ( int x,
int y ) const
inline

This function is provided for convenience.

It's equivalent to calling mapToScene(QPoint(x, y)).

Definition at line 270 of file qgraphicsview.h.

References mapToScene().

+ Here is the call graph for this function:

◆ mapToScene() [6/6]

QPolygonF QGraphicsView::mapToScene ( int x,
int y,
int w,
int h ) const
inline

This function is provided for convenience.

It's equivalent to calling mapToScene(QRect(x, y, w, h)).

Definition at line 272 of file qgraphicsview.h.

References mapToScene().

+ Here is the call graph for this function:

◆ mouseDoubleClickEvent()

◆ mouseMoveEvent()

void QGraphicsView::mouseMoveEvent ( QMouseEvent * event)
overrideprotected

\reimp

Definition at line 3282 of file qgraphicsview.cpp.

References d, ScrollHandDrag, QPoint::x(), and QPoint::y().

+ Here is the call graph for this function:

◆ mousePressEvent()

◆ mouseReleaseEvent()

◆ optimizationFlags()

QGraphicsView::OptimizationFlags QGraphicsView::optimizationFlags ( ) const

Definition at line 1456 of file qgraphicsview.cpp.

References d.

◆ paintEvent()

◆ render()

void QGraphicsView::render ( QPainter * painter,
const QRectF & target = QRectF(),
const QRect & source = QRect(),
Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio )

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:

...
view.show();
...
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
QPainter painter(&printer);
// print, fitting the viewport contents into a full page
// print the upper half of the viewport into the lower.
// half of the page.
QRect viewport = view.viewport()->rect();
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
QGraphicsScene::render()

Definition at line 2108 of file qgraphicsview.cpp.

References QRect::adjusted(), d, QPainter::device(), QPaintDevice::devType(), drawBackground(), drawForeground(), drawItems(), QTransform::fromTranslate(), QPaintDevice::height(), QRect::height(), i, Qt::IgnoreAspectRatio, Qt::IntersectClip, Qt::IntersectsItemBoundingRect, QPainter::isActive(), Qt::KeepAspectRatio, Qt::KeepAspectRatioByExpanding, QRect::left(), QTransform::map(), mapToScene(), painter, QInternal::Picture, qMax(), qMin(), QPainter::restore(), QPainter::save(), QTransform::scale(), QPainter::setClipPath(), QPainter::setClipRect(), QRect::setRect(), QPainter::setTransform(), QRect::top(), QTransform::translate(), viewport(), QPaintDevice::width(), and QRect::width().

+ Here is the call graph for this function:

◆ renderHints()

QPainter::RenderHints QGraphicsView::renderHints ( ) const

Definition at line 1288 of file qgraphicsview.cpp.

References d.

◆ resetCachedContent()

void QGraphicsView::resetCachedContent ( )

Resets any cached content.

Calling this function will clear QGraphicsView's cache. If the current cache mode is \l CacheNone, this function does nothing.

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

See also
cacheMode()

Definition at line 1624 of file qgraphicsview.cpp.

References CacheBackground, CacheNone, and d.

Referenced by setCacheMode().

+ Here is the caller graph for this function:

◆ resetTransform()

void QGraphicsView::resetTransform ( )

Resets the view transformation to the identity matrix.

See also
transform(), setTransform()

Definition at line 3881 of file qgraphicsview.cpp.

References setTransform().

Referenced by QPrintPreviewWidgetPrivate::setZoomFactor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ resizeAnchor()

QGraphicsView::ViewportAnchor QGraphicsView::resizeAnchor ( ) const

Definition at line 1393 of file qgraphicsview.cpp.

References d.

◆ resizeEvent()

void QGraphicsView::resizeEvent ( QResizeEvent * event)
overrideprotected

\reimp

Definition at line 3590 of file qgraphicsview.cpp.

References CacheBackground, d, and NoAnchor.

Referenced by QtPrivate::GraphicsView::resizeEvent().

+ Here is the caller graph for this function:

◆ rotate()

void QGraphicsView::rotate ( qreal angle)

Rotates the current view transformation angle degrees clockwise.

See also
setTransform(), transform(), scale(), shear(), translate()

Definition at line 1822 of file qgraphicsview.cpp.

References d, QTransform::rotate(), and setTransform().

Referenced by View::rotateLeft(), and View::rotateRight().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scale()

void QGraphicsView::scale ( qreal sx,
qreal sy )

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

See also
setTransform(), transform(), rotate(), shear(), translate()

Definition at line 1835 of file qgraphicsview.cpp.

References d, QTransform::scale(), and setTransform().

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), QPrintPreviewWidgetPrivate::setZoomFactor(), and QPrintPreviewWidgetPrivate::zoom().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ scene()

QGraphicsScene * QGraphicsView::scene ( ) const

Returns a pointer to the scene that is currently visualized in the view.

If no scene is currently visualized, \nullptr is returned.

See also
setScene()

Definition at line 1701 of file qgraphicsview.cpp.

References d.

Referenced by QGraphicsView(), setScene(), VideoExample::VideoGraphicsItem(), and VideoExample::VideoSurface().

+ Here is the caller graph for this function:

◆ sceneRect()

QRectF QGraphicsView::sceneRect ( ) const

Definition at line 1800 of file qgraphicsview.cpp.

References d.

◆ scrollContentsBy()

void QGraphicsView::scrollContentsBy ( int dx,
int dy )
overrideprotected

\reimp

Definition at line 3618 of file qgraphicsview.cpp.

References CacheBackground, d, dpr(), QTransform::fromScale(), FullViewportUpdate, NoViewportUpdate, QRegion::translate(), and viewport().

+ Here is the call graph for this function:

◆ setAlignment()

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

Definition at line 1334 of file qgraphicsview.cpp.

References alignment, and d.

◆ setBackgroundBrush()

void QGraphicsView::setBackgroundBrush ( const QBrush & brush)

Definition at line 2587 of file qgraphicsview.cpp.

References CacheBackground, and d.

◆ setCacheMode()

void QGraphicsView::setCacheMode ( CacheMode mode)

Definition at line 1602 of file qgraphicsview.cpp.

References d, and resetCachedContent().

+ Here is the call graph for this function:

◆ setDragMode()

void QGraphicsView::setDragMode ( DragMode mode)

Definition at line 1497 of file qgraphicsview.cpp.

References d, NoDrag, Qt::OpenHandCursor, ScrollHandDrag, and viewport().

Referenced by QPrintPreviewWidgetPrivate::init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setForegroundBrush()

void QGraphicsView::setForegroundBrush ( const QBrush & brush)

Definition at line 2617 of file qgraphicsview.cpp.

References d.

◆ setInteractive()

void QGraphicsView::setInteractive ( bool allowed)

Definition at line 1689 of file qgraphicsview.cpp.

References d.

Referenced by QPrintPreviewWidgetPrivate::init().

+ Here is the caller graph for this function:

◆ setOptimizationFlag()

void QGraphicsView::setOptimizationFlag ( OptimizationFlag flag,
bool enabled = true )

Enables flag if enabled is true; otherwise disables flag.

See also
optimizationFlags

Definition at line 1472 of file qgraphicsview.cpp.

References d.

◆ setOptimizationFlags()

void QGraphicsView::setOptimizationFlags ( OptimizationFlags flags)

Definition at line 1461 of file qgraphicsview.cpp.

References d.

◆ setRenderHint()

void QGraphicsView::setRenderHint ( QPainter::RenderHint hint,
bool enabled = true )

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

See also
renderHints

Definition at line 1308 of file qgraphicsview.cpp.

References d, and hint().

+ Here is the call graph for this function:

◆ setRenderHints()

void QGraphicsView::setRenderHints ( QPainter::RenderHints hints)

Definition at line 1293 of file qgraphicsview.cpp.

References d.

◆ setResizeAnchor()

void QGraphicsView::setResizeAnchor ( ViewportAnchor anchor)

Definition at line 1398 of file qgraphicsview.cpp.

References AnchorUnderMouse, and d.

◆ setScene()

void QGraphicsView::setScene ( QGraphicsScene * scene)

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 QGraphicsScene::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.

Definition at line 1717 of file qgraphicsview.cpp.

References QRectF::center(), connect(), d, disconnect(), isVisible(), scene(), sceneRect, QCoreApplication::sendEvent(), SIGNAL, SLOT, updateScene(), updateSceneRect(), Qt::WA_AcceptTouchEvents, QEvent::WindowActivate, and QEvent::WindowDeactivate.

Referenced by QGraphicsView(), and QPrintPreviewWidgetPrivate::init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSceneRect() [1/2]

void QGraphicsView::setSceneRect ( const QRectF & rect)

Definition at line 1809 of file qgraphicsview.cpp.

References d, and rect.

◆ setSceneRect() [2/2]

void QGraphicsView::setSceneRect ( qreal x,
qreal y,
qreal w,
qreal h )
inline

Definition at line 256 of file qgraphicsview.h.

◆ setTransform()

void QGraphicsView::setTransform ( const QTransform & matrix,
bool combine = false )

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 transforms 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.addText("GraphicsView rotated clockwise");
view.rotate(90); // the text is rendered with a 90 degree clockwise rotation
QGraphicsRectItem * addRect(const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
Creates and adds a rectangle item to the scene, and returns the item pointer.
void setRenderHints(QPainter::RenderHints hints)
void setBackgroundBrush(const QBrush &brush)
void setCacheMode(CacheMode mode)
void render(QPainter *painter, const QRectF &target=QRectF(), const QRect &source=QRect(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
Renders the source rect, which is in view coordinates, from the scene into target,...
@ SmoothPixmapTransform
Definition qpainter.h:54
@ Antialiasing
Definition qpainter.h:52
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:799
view viewport() -> scroll(dx, dy, deviceRect)
QGraphicsScene scene
[0]
QGraphicsView view
[1]

To simplify interaction 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.

See also
transform(), resetTransform(), rotate(), scale(), shear(), translate()

Definition at line 3848 of file qgraphicsview.cpp.

References d, and QTransform::isIdentity().

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), resetTransform(), rotate(), scale(), shear(), and translate().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTransformationAnchor()

void QGraphicsView::setTransformationAnchor ( ViewportAnchor anchor)

Definition at line 1366 of file qgraphicsview.cpp.

References AnchorUnderMouse, and d.

◆ setupViewport

void QGraphicsView::setupViewport ( QWidget * widget)
overrideprotectedslot

This slot is called by QAbstractScrollArea after setViewport() has been called.

Reimplement this function in a subclass of QGraphicsView to initialize the new viewport widget before it is used.

See also
setViewport()

Definition at line 2721 of file qgraphicsview.cpp.

References AnchorUnderMouse, d, QWidgetPrivate::get(), QWidget::grabGesture(), QObject::inherits(), qWarning, QWidget::setAcceptDrops(), QWidget::setAttribute(), QWidget::setAutoFillBackground(), QWidget::setFocusPolicy(), QWidget::setMouseTracking(), Qt::StrongFocus, Qt::WA_AcceptTouchEvents, and widget.

+ Here is the call graph for this function:

◆ setViewportUpdateMode()

void QGraphicsView::setViewportUpdateMode ( ViewportUpdateMode mode)

Definition at line 1432 of file qgraphicsview.cpp.

References d.

Referenced by QPrintPreviewWidgetPrivate::init().

+ Here is the caller graph for this function:

◆ shear()

void QGraphicsView::shear ( qreal sh,
qreal sv )

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

See also
setTransform(), transform(), rotate(), scale(), translate()

Definition at line 1848 of file qgraphicsview.cpp.

References d, setTransform(), and QTransform::shear().

+ Here is the call graph for this function:

◆ showEvent()

void QGraphicsView::showEvent ( QShowEvent * event)
overrideprotected

\reimp

Definition at line 3677 of file qgraphicsview.cpp.

References d.

Referenced by QtPrivate::GraphicsView::showEvent().

+ Here is the caller graph for this function:

◆ sizeHint()

QSize QGraphicsView::sizeHint ( ) const
override

\reimp

Definition at line 1262 of file qgraphicsview.cpp.

References QSizeF::boundedTo(), d, QGuiApplication::primaryScreen, and sceneRect.

+ Here is the call graph for this function:

◆ transform()

QTransform QGraphicsView::transform ( ) const

Returns the current transformation matrix for the view.

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

See also
setTransform(), rotate(), scale(), shear(), translate()

Definition at line 3790 of file qgraphicsview.cpp.

References d.

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), paintEvent(), and QPrintPreviewWidgetPrivate::setCurrentPage().

+ Here is the caller graph for this function:

◆ transformationAnchor()

QGraphicsView::ViewportAnchor QGraphicsView::transformationAnchor ( ) const

Definition at line 1361 of file qgraphicsview.cpp.

References d.

◆ translate()

void QGraphicsView::translate ( qreal dx,
qreal dy )

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

See also
setTransform(), transform(), rotate(), shear()

Definition at line 1861 of file qgraphicsview.cpp.

References d, setTransform(), and QTransform::translate().

+ Here is the call graph for this function:

◆ updateScene

void QGraphicsView::updateScene ( const QList< QRectF > & rects)
slot

Schedules an update of the scene rectangles rects.

See also
QGraphicsScene::changed()

Definition at line 2629 of file qgraphicsview.cpp.

References QRect::adjust(), boundingRect(), BoundingRectViewportUpdate, d, DontAdjustForAntialiasing, FullViewportUpdate, QRect::intersects(), NoViewportUpdate, QGRAPHICSVIEW_REGION_RECT_THRESHOLD, rect, SmartViewportUpdate, viewport(), and viewportTransform().

Referenced by setScene(), and viewportEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateSceneRect

void QGraphicsView::updateSceneRect ( const QRectF & rect)
slot

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.

See also
sceneRect, QGraphicsScene::sceneRectChanged()

Definition at line 2705 of file qgraphicsview.cpp.

References d, and rect.

Referenced by setScene().

+ Here is the caller graph for this function:

◆ viewportEvent()

◆ viewportTransform()

QTransform QGraphicsView::viewportTransform ( ) const

Returns a matrix that maps scene coordinates to viewport coordinates.

See also
mapToScene(), mapFromScene()

Definition at line 3801 of file qgraphicsview.cpp.

References d, and QTransform::fromTranslate().

Referenced by QPrintPreviewWidgetPrivate::_q_fit(), QGraphicsScenePrivate::gestureEventHandler(), items(), items(), items(), items(), paintEvent(), QScroller::pixelPerMeter(), and updateScene().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ viewportUpdateMode()

QGraphicsView::ViewportUpdateMode QGraphicsView::viewportUpdateMode ( ) const

Definition at line 1427 of file qgraphicsview.cpp.

References d.

Friends And Related Symbol Documentation

◆ QGraphicsItemPrivate

friend class QGraphicsItemPrivate
friend

Definition at line 250 of file qgraphicsview.h.

◆ QGraphicsScene

friend class QGraphicsScene
friend

Definition at line 248 of file qgraphicsview.h.

◆ QGraphicsScenePrivate

friend class QGraphicsScenePrivate
friend

Definition at line 249 of file qgraphicsview.h.

Property Documentation

◆ alignment

QGraphicsView::alignment
readwrite

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 Qt::AlignCenter, which is default, the scene will be centered in the view, and if the alignment is (Qt::AlignLeft | Qt::AlignTop), the scene will be rendered in the top-left corner of the view.

Definition at line 31 of file qgraphicsview.h.

Referenced by setAlignment().

◆ backgroundBrush

QGraphicsView::backgroundBrush
readwrite

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 Qt::NoBrush pattern.

See also
QGraphicsScene::backgroundBrush, foregroundBrush

Definition at line 27 of file qgraphicsview.h.

◆ cacheMode

QGraphicsView::cacheMode
readwrite

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:

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

Definition at line 34 of file qgraphicsview.h.

◆ dragMode

QGraphicsView::dragMode
readwrite

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().

Definition at line 33 of file qgraphicsview.h.

◆ foregroundBrush

QGraphicsView::foregroundBrush
readwrite

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 Qt::NoBrush pattern.

See also
QGraphicsScene::foregroundBrush, backgroundBrush

Definition at line 28 of file qgraphicsview.h.

◆ interactive

QGraphicsView::interactive
readwrite

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.

Definition at line 29 of file qgraphicsview.h.

◆ optimizationFlags

QGraphicsView::optimizationFlags
readwrite

flags that can be used to tune QGraphicsView's performance.

Since
4.3

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.

See also
setOptimizationFlag()

Definition at line 44 of file qgraphicsview.h.

◆ renderHints

QGraphicsView::renderHints
readwrite

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.

QPainter::TextAntialiasing is enabled by default.

Example:

Definition at line 32 of file qgraphicsview.h.

◆ resizeAnchor

QGraphicsView::resizeAnchor
readwrite

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 \l alignment to position the scene in the view.

See also
alignment, transformationAnchor

Definition at line 37 of file qgraphicsview.h.

◆ sceneRect

QGraphicsView::sceneRect
readwrite

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 QGraphicsScene::sceneRect, and it changes with QGraphicsScene::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
QGraphicsScene::sceneRect

Definition at line 30 of file qgraphicsview.h.

Referenced by fitInView(), setScene(), and sizeHint().

◆ transformationAnchor

QGraphicsView::transformationAnchor
readwrite

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 \l alignment to position the scene in the view.

See also
alignment, resizeAnchor

Definition at line 35 of file qgraphicsview.h.

◆ viewportUpdateMode

QGraphicsView::viewportUpdateMode
readwrite

how the viewport should update its contents.

Since
4.3

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

Definition at line 38 of file qgraphicsview.h.


The documentation for this class was generated from the following files: