|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QGraphicsView Class Reference
[QtGui module]

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

     #include <QGraphicsView>

Inherits QAbstractScrollArea.

This class was introduced in Qt 4.2.

- List of all members, including inherited members

### Public Types

- flags CacheMode
- enum CacheModeFlag { CacheNone, CacheBackground }
- enum DragMode { NoDrag, ScrollHandDrag, RubberBandDrag }
- enum ViewportAnchor { NoAnchor, AnchorViewCenter, AnchorUnderMouse }

### Properties

alignment : Qt::Alignment backgroundBrush : QBrush cacheMode : CacheMode dragMode : DragMode foregroundBrush : QBrush | interactive : bool renderHints : QPainter::RenderHints resizeAnchor : ViewportAnchor sceneRect : QRectF transformationAnchor : ViewportAnchor

- 2 properties inherited from QAbstractScrollArea
- 6 properties inherited from QFrame
- 55 properties inherited from QWidget
- 1 property inherited from QObject

### Public Functions

- QGraphicsView ( QWidget * parent = 0 )
- QGraphicsView ( QGraphicsScene * scene, QWidget * parent = 0 )
- ~QGraphicsView ()
- Qt::Alignment alignment () const
- QBrush backgroundBrush () const
- CacheMode cacheMode () const
- void centerOn ( const QPointF & pos )
- void centerOn ( qreal x, qreal y )
- void centerOn ( const QGraphicsItem * item )
- DragMode dragMode () const
- void ensureVisible ( const QRectF & rect, int xmargin = 50, int ymargin = 50 )
- void ensureVisible ( qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50 )
- void ensureVisible ( const QGraphicsItem * item, int xmargin = 50, int ymargin = 50 )
- void fitInView ( const QRectF & rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
- void fitInView ( qreal x, qreal y, qreal w, qreal h, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
- void fitInView ( const QGraphicsItem * item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
- QBrush foregroundBrush () const
- bool isInteractive () const
- QGraphicsItem * itemAt ( const QPoint & pos ) const
- QGraphicsItem * itemAt ( int x, int y ) const
- QList<QGraphicsItem *> items () const
- QList<QGraphicsItem *> items ( const QPoint & pos ) const
- QList<QGraphicsItem *> items ( int x, int y ) const
- QList<QGraphicsItem *> items ( const QRect & rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QList<QGraphicsItem *> items ( const QPolygon & polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QList<QGraphicsItem *> items ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QPoint mapFromScene ( const QPointF & point ) const
- QPolygon mapFromScene ( const QRectF & rect ) const
- QPolygon mapFromScene ( const QPolygonF & polygon ) const
- QPainterPath mapFromScene ( const QPainterPath & path ) const
- QPoint mapFromScene ( qreal x, qreal y ) const
- QPolygon mapFromScene ( qreal x, qreal y, qreal w, qreal h ) const
- QPointF mapToScene ( const QPoint & point ) const
- QPolygonF mapToScene ( const QRect & rect ) const
- QPolygonF mapToScene ( const QPolygon & polygon ) const
- QPainterPath mapToScene ( const QPainterPath & path ) const
- QPointF mapToScene ( int x, int y ) const
- QPolygonF mapToScene ( int x, int y, int w, int h ) const
- QMatrix matrix () const
- void render ( QPainter * painter, const QRectF & target = QRectF(), const QRect & source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio )
- QPainter::RenderHints renderHints () const
- void resetCachedContent ()
- void resetMatrix ()
- ViewportAnchor resizeAnchor () const
- void rotate ( qreal angle )
- void scale ( qreal sx, qreal sy )
- QGraphicsScene * scene () const
- QRectF sceneRect () const
- void setAlignment ( Qt::Alignment alignment )
- void setBackgroundBrush ( const QBrush & brush )
- void setCacheMode ( CacheMode mode )
- void setDragMode ( DragMode mode )
- void setForegroundBrush ( const QBrush & brush )
- void setInteractive ( bool allowed )
- void setMatrix ( const QMatrix & matrix, bool combine = false )
- void setRenderHint ( QPainter::RenderHint hint, bool enabled = true )
- void setRenderHints ( QPainter::RenderHints hints )
- void setResizeAnchor ( ViewportAnchor anchor )
- void setScene ( QGraphicsScene * scene )
- void setSceneRect ( const QRectF & rect )
- void setSceneRect ( qreal x, qreal y, qreal w, qreal h )
- void setTransformationAnchor ( ViewportAnchor anchor )
- void shear ( qreal sh, qreal sv )
- ViewportAnchor transformationAnchor () const
- void translate ( qreal dx, qreal dy )

- 15 public functions inherited from QAbstractScrollArea
- 13 public functions inherited from QFrame
- 195 public functions inherited from QWidget
- 29 public functions inherited from QObject
- 12 public functions inherited from QPaintDevice

### Public Slots

- void updateScene ( const QList<QRectF> & rects )
- void updateSceneRect ( const QRectF & rect )

- 19 public slots inherited from QWidget
- 1 public slot inherited from QObject

### Protected Functions

- virtual void drawBackground ( QPainter * painter, const QRectF & rect )
- virtual void drawForeground ( QPainter * painter, const QRectF & rect )
- virtual void drawItems ( QPainter * painter, int numItems, QGraphicsItem *[] items, const QStyleOptionGraphicsItem[] options )

- 16 protected functions inherited from QAbstractScrollArea
- 38 protected functions inherited from QWidget
- 7 protected functions inherited from QObject
- 1 protected function inherited from QPaintDevice

### Protected Slots

- void setupViewport ( QWidget * widget )

- 1 protected slot inherited from QAbstractScrollArea
- 1 protected slot inherited from QWidget

### Additional Inherited Members

- 1 signal inherited from QWidget
- 1 signal inherited from QObject
- 4 static public members inherited from QWidget
- 5 static public members inherited from QObject

## Detailed Description

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

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:

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

     QGraphicsView view(&scene);
     view.show();

You can explicitly scroll to any position on the scene by using the scrollbars, 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 scrollbars' ranges appropriately.

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 QGLWidget). QGraphicsView takes ownership of the viewport widget.

QGraphicsView supports affine transformations, using QMatrix. You can either pass a matrix to setMatrix(), 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.

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.

See also QGraphicsScene, QGraphicsItem, and QGraphicsSceneEvent.

## Member Type Documentation

### enum QGraphicsView::CacheModeFlag
flags QGraphicsView::CacheMode

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

Constant | Value | Description
--- | --- | ---
`QGraphicsView::CacheNone` | `0x0` | All painting is done directly onto the viewport.
`QGraphicsView::CacheBackground` | `0x1` | 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.

The CacheMode type is a typedef for QFlags<CacheModeFlag>. It stores an OR combination of CacheModeFlag values.

See also cacheMode.

### enum QGraphicsView::DragMode

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

Constant | Value | Description
--- | --- | ---
`QGraphicsView::NoDrag` | `0` | Nothing happens; the mouse event is ignored.
`QGraphicsView::ScrollHandDrag` | `1` | The cursor changes into a pointing hand, and dragging the mouse around will scroll the scrolbars.
`QGraphicsView::RubberBandDrag` | `2` | A rubber band will appear. Dragging the mouse will set the rubber band geometry, and all items covered by the rubber band are selected.

See also dragMode and QGraphicsScene::setSelectionArea().

### enum 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 | Value | Description
--- | --- | ---
`QGraphicsView::NoAnchor` | `0` | No anchor, i.e. the view leaves the scene's position unchanged.
`QGraphicsView::AnchorViewCenter` | `1` | The scene point at the center of the view is used as the anchor.
`QGraphicsView::AnchorUnderMouse` | `2` | The point under the mouse is used as the anchor.

See also resizeAnchor and transformationAnchor.

## Property Documentation

### alignment : Qt::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 scrollbars,) 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.

Access functions:

- Qt::Alignment alignment () const
- void setAlignment ( Qt::Alignment alignment )

### backgroundBrush : 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.

Access functions:

- QBrush backgroundBrush () const
- void setBackgroundBrush ( const QBrush & brush )

See also QGraphicsScene::backgroundBrush and foregroundBrush.

### cacheMode : 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:

     QGraphicsView view;
     view.setBackgroundBrush(":/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.

Access functions:

- CacheMode cacheMode () const
- void setCacheMode ( CacheMode mode )

See also resetCachedContent() and QPixmapCache.

### dragMode : 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().

Access functions:

- DragMode dragMode () const
- void setDragMode ( DragMode mode )

### foregroundBrush : 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 background drawing for this view, you can reimplement drawBackground() instead.

Access functions:

- QBrush foregroundBrush () const
- void setForegroundBrush ( const QBrush & brush )

See also QGraphicsScene::foregroundBrush and backgroundBrush.

### interactive : bool

This property holds whether the view allowed 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).

Access functions:

- bool isInteractive () const
- void setInteractive ( bool allowed )

### renderHints : QPainter::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.

QPainter::TextAntialiasing is enabled by default.

Example:

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

     QGraphicsView view(&scene);
     view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
     view.show();

Access functions:

- QPainter::RenderHints renderHints () const
- void setRenderHints ( QPainter::RenderHints hints )

### resizeAnchor : 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 scrollbars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

Access functions:

- ViewportAnchor resizeAnchor () const
- void setResizeAnchor ( ViewportAnchor anchor )

See also alignment, transformationAnchor, and Qt::WNorthWestGravity.

### sceneRect : QRectF

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

The scene rect 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: The maximum size of the view's scene rect is limited by the range of QAbstractScrollArea's scrollbars, which operate in integer coordinates.

Access functions:

- QRectF sceneRect () const
- void setSceneRect ( const QRectF & rect )
- void setSceneRect ( qreal x, qreal y, qreal w, qreal h )

See also QGraphicsScene::sceneRect.

### transformationAnchor : 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 scrollbars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.

Access functions:

- ViewportAnchor transformationAnchor () const
- void setTransformationAnchor ( ViewportAnchor anchor )

See also alignment and resizeAnchor.

## Member Function Documentation

### QGraphicsView::QGraphicsView ( QWidget * parent = 0 )

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

### QGraphicsView::QGraphicsView ( QGraphicsScene * scene, QWidget * parent = 0 )

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

### QGraphicsView::~QGraphicsView ()

Destructs the QGraphicsView object.

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

See also ensureVisible().

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

This is an overloaded member function, provided for convenience.

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

### void QGraphicsView::centerOn ( const QGraphicsItem * item )

This is an overloaded member function, provided for convenience.

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

See also ensureVisible().

### void QGraphicsView::drawBackground ( QPainter * painter, const QRectF & rect ) `[virtual protected]`

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() and QGraphicsScene::drawForeground().

### void QGraphicsView::drawForeground ( QPainter * painter, const QRectF & rect ) `[virtual protected]`

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() and QGraphicsScene::drawBackground().

### void QGraphicsView::drawItems ( QPainter * painter, int numItems, QGraphicsItem *[] items, const QStyleOptionGraphicsItem[] options ) `[virtual protected]`

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.

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

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

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

This is an overloaded member function, provided for convenience.

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

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

This is an overloaded member function, provided for convenience.

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

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

See also setMatrix(), ensureVisible(), and centerOn().

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

This is an overloaded member function, provided for convenience.

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

See also ensureVisible() and centerOn().

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

This is an overloaded member function, provided for convenience.

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

See also ensureVisible() and centerOn().

### 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 * QGraphicsView::itemAt ( int x, int y ) const

This is an overloaded member function, provided for convenience.

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

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

Returns a list of all the items in the associated scene.

See also QGraphicsScene::items().

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

This is an overloaded member function, provided for convenience.

Returns a list of all the items at the position pos in the view. The items are listed in descending Z order (i.e., the first item in the list is the top-most item, and the last item is the bottom-most 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() and QGraphicsItem::zValue().

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

This is an overloaded member function, provided for convenience.

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

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

This is an overloaded member function, provided for convenience.

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.

See also itemAt(), items(), and mapToScene().

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

This is an overloaded member function, provided for convenience.

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.

See also itemAt(), items(), and mapToScene().

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

This is an overloaded member function, provided for convenience.

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(), and mapToScene().

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

Returns the scene coordinate point to viewport coordinates.

See also mapToScene().

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

This is an overloaded member function, provided for convenience.

Returns the scene rectangle rect to a viewport coordinate polygon.

See also mapToScene().

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

This is an overloaded member function, provided for convenience.

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

See also mapToScene().

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

This is an overloaded member function, provided for convenience.

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

See also mapToScene().

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

This is an overloaded member function, provided for convenience.

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

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

This is an overloaded member function, provided for convenience.

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

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

Returns the viewport coordinate point mapped to scene coordinates.

See also mapFromScene().

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

This is an overloaded member function, provided for convenience.

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

See also mapFromScene().

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

This is an overloaded member function, provided for convenience.

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

See also mapFromScene().

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

This is an overloaded member function, provided for convenience.

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

See also mapFromScene().

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

This is an overloaded member function, provided for convenience.

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

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

This is an overloaded member function, provided for convenience.

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

### QMatrix QGraphicsView::matrix () const

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

See also setMatrix(), rotate(), scale(), shear(), and translate().

### 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:

     QGraphicsScene scene;
     scene.addItem(...
     ...

     QGraphicsView view(&scene);
     view.show();
     ...

     QPrinter printer(QPrinter::HighResolution);
     printer.setPageSize(QPrinter::A4);
     QPainter painter(&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.
     QRect 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 QGraphicsScene::render().

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

### void QGraphicsView::resetMatrix ()

Resets the view transformation matrix to the identity matrix.

### void QGraphicsView::rotate ( qreal angle )

Rotates the current view transformation angle degrees clockwise.

See also setMatrix(), matrix(), scale(), shear(), and translate().

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

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

See also setMatrix(), matrix(), rotate(), shear(), and translate().

### QGraphicsScene * QGraphicsView::scene () const

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

See also setScene().

### void QGraphicsView::setMatrix ( const QMatrix & 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 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:

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

     QGraphicsView view(&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.

See also matrix(), rotate(), scale(), shear(), and translate().

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

### 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 scrollbars are adjusted to fit the size of the scene.

See also scene().

### void QGraphicsView::setupViewport ( QWidget * widget ) `[protected slot]`

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

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

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

See also setMatrix(), matrix(), rotate(), scale(), and translate().

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

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

See also setMatrix(), matrix(), rotate(), and shear().

### void QGraphicsView::updateScene ( const QList<QRectF> & rects ) `[slot]`

Schedules an update of the scene rectangles rects.

See also QGraphicsScene::changed().

### 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 and QGraphicsScene::sceneRectChanged().