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

# QGraphicsScene Class Reference
[QtGui module]

The QGraphicsScene class provides a surface for managing a large number of 2D graphical items. More...

     #include <QGraphicsScene>

Inherits QObject.

This class was introduced in Qt 4.2.

- List of all members, including inherited members

### Public Types

- enum ItemIndexMethod { BspTreeIndex, NoIndex }

### Properties

- backgroundBrush : QBrush
- foregroundBrush : QBrush
- itemIndexMethod : ItemIndexMethod
- sceneRect : QRectF

- 1 property inherited from QObject

### Public Functions

- QGraphicsScene ( QObject * parent = 0 )
- QGraphicsScene ( const QRectF & sceneRect, QObject * parent = 0 )
- QGraphicsScene ( qreal x, qreal y, qreal width, qreal height, QObject * parent = 0 )
- virtual ~QGraphicsScene ()
- QGraphicsEllipseItem * addEllipse ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
- void addItem ( QGraphicsItem * item )
- QGraphicsLineItem * addLine ( const QLineF & line, const QPen & pen = QPen() )
- QGraphicsPathItem * addPath ( const QPainterPath & path, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
- QGraphicsPixmapItem * addPixmap ( const QPixmap & pixmap )
- QGraphicsPolygonItem * addPolygon ( const QPolygonF & polygon, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
- QGraphicsRectItem * addRect ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )
- QGraphicsTextItem * addText ( const QString & text, const QFont & font = QFont() )
- QBrush backgroundBrush () const
- void clearFocus ()
- void clearSelection ()
- QList<QGraphicsItem *> collidingItems ( const QGraphicsItem * item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QGraphicsItemGroup * createItemGroup ( const QList<QGraphicsItem *> & items )
- void destroyItemGroup ( QGraphicsItemGroup * group )
- QGraphicsItem * focusItem () const
- QBrush foregroundBrush () const
- bool hasFocus () const
- qreal height () const
- virtual QVariant inputMethodQuery ( Qt::InputMethodQuery query ) const
- QGraphicsItem * itemAt ( const QPointF & position ) const
- QGraphicsItem * itemAt ( qreal x, qreal y ) const
- ItemIndexMethod itemIndexMethod () const
- QList<QGraphicsItem *> items () const
- QList<QGraphicsItem *> items ( const QPointF & pos ) const
- QList<QGraphicsItem *> items ( const QRectF & rectangle, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QList<QGraphicsItem *> items ( const QPolygonF & polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QList<QGraphicsItem *> items ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QRectF itemsBoundingRect () const
- QGraphicsItem * mouseGrabberItem () const
- void removeItem ( QGraphicsItem * item )
- void render ( QPainter * painter, const QRectF & target = QRectF(), const QRectF & source = QRectF(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio )
- QRectF sceneRect () const
- QList<QGraphicsItem *> selectedItems () const
- void setBackgroundBrush ( const QBrush & brush )
- void setFocus ( Qt::FocusReason focusReason = Qt::OtherFocusReason )
- void setFocusItem ( QGraphicsItem * item, Qt::FocusReason focusReason = Qt::OtherFocusReason )
- void setForegroundBrush ( const QBrush & brush )
- void setItemIndexMethod ( ItemIndexMethod method )
- void setSceneRect ( const QRectF & rect )
- void setSceneRect ( qreal x, qreal y, qreal w, qreal h )
- void setSelectionArea ( const QPainterPath & path )
- QList<QGraphicsView *> views () const
- qreal width () const

- 29 public functions inherited from QObject

### Public Slots

- void advance ()
- void update ( const QRectF & rect = QRectF() )

- 1 public slot inherited from QObject

### Signals

- void changed ( const QList<QRectF> & region )
- void sceneRectChanged ( const QRectF & rect )

- 1 signal inherited from QObject

### Protected Functions

- virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * contextMenuEvent )
- virtual void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
- 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, QWidget * widget = 0 )
- virtual void dropEvent ( QGraphicsSceneDragDropEvent * event )
- virtual bool event ( QEvent * event )
- virtual void focusInEvent ( QFocusEvent * focusEvent )
- virtual void focusOutEvent ( QFocusEvent * focusEvent )
- virtual void helpEvent ( QGraphicsSceneHelpEvent * helpEvent )
- virtual void inputMethodEvent ( QInputMethodEvent * event )
- virtual void keyPressEvent ( QKeyEvent * keyEvent )
- virtual void keyReleaseEvent ( QKeyEvent * keyEvent )
- virtual void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * mouseEvent )
- virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent )
- virtual void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
- virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent )
- virtual void wheelEvent ( QGraphicsSceneWheelEvent * wheelEvent )

- 7 protected functions inherited from QObject

### Additional Inherited Members

- 5 static public members inherited from QObject

## Detailed Description

The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.

The class serves as a container for QGraphicsItems. It is used together with QGraphicsView for visualizing graphical items, such as lines, rectangles, text, or even custom items, on a 2D surface. QGraphicsScene is part of The Graphics View Framework.

QGraphicsScene also provides functionality that lets you efficiently determine both the location of items, and for determining what items are visible within an arbitrary area on the scene. With the QGraphicsView widget, you can either visualize the whole scene, or zoom in and view only parts of the scene.

Example:

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

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

Note that QGraphicsScene has no visual appearance of its own; it only manages the items. You need to create a QGraphicsView widget to visualize the scene.

To add items to a scene, you start off by constructing a QGraphicsScene object. Then, you have two options: either add your existing QGraphicsItem objects by calling addItem(), or you can call one of the convenience functions addEllipse(), addLine(), addPath(), addPixmap(), addPolygon(), addRect(), or addText(), which all return a pointer to the newly added item. You can then visualize the scene using QGraphicsView. When the scene changes, (e.g., when an item moves or is transformed) QGraphicsScene emits the changed() signal. To remove an item, call removeItem().

QGraphicsScene uses an indexing algorithm to manage the location of items efficiently. By default, a BSP (Binary Space Partitioning) tree is used; an algorithm suitable for large scenes where most items remain static (i.e., do not move around). You can choose to disable this index by calling setItemIndexMethod(). For more information about the available indexing algorithms, see the itemIndexMethod propery.

The scene's bounding rect is set by calling setSceneRect(). Items can be placed at any position on the scene, and the size of the scene is by default unlimited. The scene rect is used only for internal bookkeeping, maintaining the scene's item index. If the scene rect is unset, QGraphicsScene will use the bounding area of all items, as returned by itemsBoundingRect(), as the scene rect. However, itemsBoundingRect() is a relatively time consuming function, as it operates by collecting positional information for every item on the scene. Because of this, you should always set the scene rect when operating on large scenes.

One of QGraphicsScene's greatest strengts is its ability to efficiently determine the location of items. Even with millions of items on the scene, the items() functions can determine the location of an item within few milliseconds. There are several overloads to items(): one that finds items at a certain position, one that finds items inside or intersecting with a polygon or a rectangle, and more. The list of returned items is sorted by stacking order, with the topmost item being the first item in the list. For convenience, there is also an itemAt() function that returns the topmost item at a given position.

QGraphicsScene maintains selection information for the scene. To select items, call setSelectionArea(), and to clear the current selection, call clearSelection(). Call selectedItems() to get the list of all selected items.

Another responsibility that QGraphicsScene has, is to propagate events from QGraphicsView. To send an event to a scene, you construct an event that inherits QEvent, and then send it using, for example, QApplication::sendEvent(). event() is responsible for dispatching the event to the individual items. Some common events are handled by convenience event handlers. For example, key press events are handled by keyPressEvent(), and mouse press events are handled by mousePressEvent().

Key events are delivered to the focus item. To set the focus item, you can either call setFocusItem(), passing an item that accepts focus, or the item itself can call QGraphicsItem::setFocus(). Call focusItem() to get the current focus item. For compatibility with widgets, the scene also maintains its own focus information. By default, the scene does not have focus, and all key events are discarded. If setFocus() is called, or if an item on the scene gains focus, the scene automatically gains focus. If the scene has focus, hasFocus() will return true, and key events will be forwarded to the focus item, if any. If the scene loses focus, (i.e., someone calls clearFocus(),) while an item has focus, the scene will maintain its item focus information, and once the scene regains focus, it will make sure the last focus item regains focus.

For mouse-over effects, QGraphicsScene dispatches hover events. If an item accepts hover events (see QGraphicsItem::acceptsHoverEvents()), it will receive a GraphicsSceneHoverEnter event when the mouse enters its area. As the mouse continues moving inside the item's area, QGraphicsScene will send it GraphicsSceneHoverMove events. When the mouse leaves the item's area, the item will receive a GraphicsSceneHoverLeave event.

All mouse events are delivered to the current mouse grabber item. An item becomes the scene's mouse grabber if it accepts mouse events (see QGraphicsItem::acceptedMouseButtons()) and it receives a mouse press. It stays the mouse grabber until it receives a mouse release when no other mouse buttons are pressed. You can call mouseGrabberItem() to determine what item is currently grabbing the mouse.

See also QGraphicsItem and QGraphicsView.

## Member Type Documentation

### enum QGraphicsScene::ItemIndexMethod

This enum describes the indexing algorithms QGraphicsScene provides for managing positional information about items on the scene.

Constant | Value | Description
--- | --- | ---
`QGraphicsScene::BspTreeIndex` | `0` | A Binary Space Partitioning tree is applied. All QGraphicsScene's item location algorithms are of an order close to logarithmic complexity, by making use of binary search. Adding, moving and removing items is logarithmic. This approach is best for static scenes (i.e., scenes where most items do not move).
`QGraphicsScene::NoIndex` | `-1` | No index is applied. Item location is of linear complexity, as all items on the scene are searched. Adding, moving and removing items, however, is done in constant time. This approach is ideal for dynamic scenes, where many items are added, moved or removed continuously.

## Property Documentation

### backgroundBrush : QBrush

This property holds the background brush of the scene.

Set this property to changes the scene's background to a different color, gradient or texture. The default background brush is Qt::NoBrush. The background is drawn before (behind) the items.

Example:

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

     // a blue background
     scene.setBackgroundBrush(Qt::blue);

     // a gradient background
     QRadialGradient gradient(0, 0, 10);
     gradient.setSpread(QGradient::RepeatSpread);
     scene.setBackgroundBrush(gradient);

QGraphicsScene::render() calls drawBackground() to draw the scene background. For more detailed control over how the background is drawn, you can reimplement drawBackground() in a subclass of QGraphicsScene.

Access functions:

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

### foregroundBrush : QBrush

This property holds the foreground brush of the scene.

Change this property to set the scene's foreground to a different color, gradient or texture.

The foreground is drawn after (on top of) the items. The default foreground brush is Qt::NoBrush ( i.e. the foreground is not drawn).

Example:

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

     // a white semi-transparent foreground
     scene.setForegroundBrush(QColor(255, 255, 255, 127));

     // a grid foreground
     scene.setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern));

QGraphicsScene::render() calls drawForeground() to draw the scene foreground. For more detailed control over how the foreground is drawn, you can reimplement the drawForeground() function in a QGraphicsScene subclass.

Access functions:

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

### itemIndexMethod : ItemIndexMethod

This property holds the item indexing method.

QGraphicsScene applies an indexing algorithm to the scene, to speed up item discovery functions like items() and itemAt(). Indexing is most efficient for static scenes (i.e., where items don't move around). For dynamic scenes, or scenes with many animated items, the index bookkeeping can outweight the fast lookup speeds.

For the common case, the default index method BspTreeIndex works fine. If your scene uses many animations and you are experiencing slowness, you can disable indexing by calling `setItemIndexMethod(NoIndex)`.

Access functions:

- ItemIndexMethod itemIndexMethod () const
- void setItemIndexMethod ( ItemIndexMethod method )

### sceneRect : QRectF

This property holds the scene rectangle; the bounding rectangle of the scene.

The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.

If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).

Access functions:

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

See also width(), height(), and QGraphicsView::sceneRect.

## Member Function Documentation

### QGraphicsScene::QGraphicsScene ( QObject * parent = 0 )

Constructs a QGraphicsScene object. The parent parameter is passed to QObject's constructor.

### QGraphicsScene::QGraphicsScene ( const QRectF & sceneRect, QObject * parent = 0 )

Constructs a QGraphicsScene object, using sceneRect for its scene rectangle. The parent parameter is passed to QObject's constructor.

See also sceneRect.

### QGraphicsScene::QGraphicsScene ( qreal x, qreal y, qreal width, qreal height, QObject * parent = 0 )

Constructs a QGraphicsScene object, using the rectangle specified by (x, y), and the given width and height for its scene rectangle. The parent parameter is passed to QObject's constructor.

See also sceneRect.

### QGraphicsScene::~QGraphicsScene () `[virtual]`

Destroys the QGraphicsScene object.

### QGraphicsEllipseItem * QGraphicsScene::addEllipse ( const QRectF & rect, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

Creates and adds an ellipse item to the scene, and returns the item pointer. The geometry of the ellipse is defined by rect, and it's pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addLine(), addPath(), addPixmap(), addRect(), addText(), and addItem().

### void QGraphicsScene::addItem ( QGraphicsItem * item )

Adds the item item and all its childen to the scene.

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

If the item is already associated with a scene, it will first be removed from that scene, and then added to this scene.

See also removeItem(), addEllipse(), addLine(), addPath(), addPixmap(), addRect(), and addText().

### QGraphicsLineItem * QGraphicsScene::addLine ( const QLineF & line, const QPen & pen = QPen() )

Creates and adds a line item to the scene, and returns the item pointer. The geometry of the line is defined by line, and it's pen is initialized to pen.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addPath(), addPixmap(), addRect(), addText(), and addItem().

### QGraphicsPathItem * QGraphicsScene::addPath ( const QPainterPath & path, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

Creates and adds a path item to the scene, and returns the item pointer. The geometry of the path is defined by path, and it's pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addLine(), addPixmap(), addRect(), addText(), and addItem().

### QGraphicsPixmapItem * QGraphicsScene::addPixmap ( const QPixmap & pixmap )

Creates and adds a pixmap item to the scene, and returns the item pointer. The pixmap is defined by pixmap.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addLine(), addPath(), addRect(), addText(), and addItem().

### QGraphicsPolygonItem * QGraphicsScene::addPolygon ( const QPolygonF & polygon, const QPen & pen = QPen(), const QBrush & brush = QBrush() )

Creates and adds a polygon item to the scene, and returns the item pointer. The polygon is defined by polygon, and it's pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addLine(), addPath(), addRect(), addText(), and addItem().

### QGraphicsRectItem * QGraphicsScene::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. The geometry of the rectangle is defined by rect, and it's pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addLine(), addPixmap(), addPixmap(), addText(), and addItem().

### QGraphicsTextItem * QGraphicsScene::addText ( const QString & text, const QFont & font = QFont() )

Creates and adds a text item to the scene, and returns the item pointer. The text string is initialized to text, and it's font is initialized to font.

The item's position is initialized to (0, 0).

If the item is visible (i.e., QGraphicsItem::isVisible() returns true), QGraphicsScene will emit changed() once control goes back to the event loop.

See also addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), and addItem().

### void QGraphicsScene::advance () `[slot]`

This slot advances the scene by one step, by calling QGraphicsItem::advance() for all items on the scene. This is done in two phases: in the first phase, all items are notified that the scene is about to change, and in the second phase all items are notified that they can move. In the first phase, QGraphicsItem::advance() is called passing a value of 0 as an argument, and 1 is passed in the second phase.

See also QGraphicsItem::advance(), QGraphicsItemAnimation, and QTimeLine.

### void QGraphicsScene::changed ( const QList<QRectF> & region ) `[signal]`

This signal is emitted by QGraphicsScene when control reaches the event loop, if the scene content changes. The region parameter contains a list of scene rectangles that indicate the area that has been changed.

See also QGraphicsView::update().

### void QGraphicsScene::clearFocus ()

Clears focus from the scene. If any item has focus when this function is called, it will lose focus, and regain focus again once the scene regains focus.

A scene that does not have focus ignores key events.

See also hasFocus(), setFocus(), and setFocusItem().

### void QGraphicsScene::clearSelection ()

Clears the current selection.

See also setSelectionArea() and selectedItems().

### QList<QGraphicsItem *> QGraphicsScene::collidingItems ( const QGraphicsItem * item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

Returns a list of all items that collide with item. Collisions are determined by calling QGraphicsItem::collidesWithItem(); the collision detection is determined by mode. By default, all items whose shape intersects item or is contained inside item's shape are returned.

The items are returned 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).

See also items(), itemAt(), and QGraphicsItem::collidesWithItem().

### void QGraphicsScene::contextMenuEvent ( QGraphicsSceneContextMenuEvent * contextMenuEvent ) `[virtual protected]`

This event handler, for event contextMenuEvent, can be reimplemented in a subclass to receive context menu events. The default implementation forwards the event to the item at the scene position provided by the event.

See also QGraphicsItem::contextMenuEvent().

### QGraphicsItemGroup * QGraphicsScene::createItemGroup ( const QList<QGraphicsItem *> & items )

Groups all items in items into a new QGraphicsItemGroup, and returns a pointer to the group. The group is created with the common ancestor of items as its parent, and with position (0, 0). The items are all reparented to the group, and their positions and transformations are mapped to the group. If items is empty, this function will return an empty top-level QGraphicsItemGroup.

QGraphicsScene has ownership of the group item; you do not need to delete it. To dismantle (ungroup) a group, call destroyItemGroup().

See also destroyItemGroup() and QGraphicsItemGroup::addToGroup().

### void QGraphicsScene::destroyItemGroup ( QGraphicsItemGroup * group )

Reparents all items in group to group's parent item, then removes group from the scene, and finally deletes it. The items' positions and transformations are mapped from the group to the group's parent.

See also createItemGroup() and QGraphicsItemGroup::removeFromGroup().

### void QGraphicsScene::dragEnterEvent ( QGraphicsSceneDragDropEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented in a subclass to receive drag enter events for the scene.

The default implementation accepts the event and prepares the scene to accept drag move events.

See also QGraphicsItem::dragEnterEvent(), dragMoveEvent(), dragLeaveEvent(), and dropEvent().

### void QGraphicsScene::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented in a subclass to receive drag leave events for the scene.

See also QGraphicsItem::dragLeaveEvent(), dragEnterEvent(), dragMoveEvent(), and dropEvent().

### void QGraphicsScene::dragMoveEvent ( QGraphicsSceneDragDropEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented in a subclass to receive drag move events for the scene.

See also QGraphicsItem::dragMoveEvent(), dragEnterEvent(), dragLeaveEvent(), and dropEvent().

### void QGraphicsScene::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 the scene.

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

See also drawForeground() and drawItems().

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

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

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

See also drawBackground() and drawItems().

### void QGraphicsScene::drawItems ( QPainter * painter, int numItems, QGraphicsItem *[] items, const QStyleOptionGraphicsItem[] options, QWidget * widget = 0 ) `[virtual protected]`

Paints the given items using the provided painter, after the background has been drawn, and before the foreground has been drawn. All painting is done in scene coordinates. Before drawing each item, the painter must be transformed using QGraphicsItem::sceneMatrix().

The options parameter is the list of style option objects for each item in items. The numItems parameter is the number of items in items and options in options. The widget parameter is optional; if specified, it should point to the widget that is being painted on.

The default implementation prepares the painter matrix, and calls QGraphicsItem::paint() on all items. Reimplement this function to provide custom painting of all items for the scene; gaining complete control over how each item is drawn. In some cases this can increase drawing performance significantly.

Example:

     void CustomScene::drawItems(QPainter *painter,
                                 int numItems,
                                 QGraphicsItem *items[],
                                 const QStyleOptionGraphicsItem options[]
                                 QWidget *widget)
     {
         for (int i = 0; i < numItems; ++i) {
             // Draw the item
             painter->save();
             painter->setMatrix(items[i]->sceneMatrix(), true);
             items.at(i)->paint(painter, options[i], widget);
             painter->restore();
         }
     }

See also drawBackground() and drawForeground().

### void QGraphicsScene::dropEvent ( QGraphicsSceneDragDropEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented in a subclass to receive drop events for the scene.

See also QGraphicsItem::dropEvent(), dragEnterEvent(), dragMoveEvent(), and dragLeaveEvent().

### bool QGraphicsScene::event ( QEvent * event ) `[virtual protected]`

Processes the event event, and dispatches it to the respective event handlers.

In addition to calling the convenience event handlers, this function is responsible for converting mouse move events to hover events for when there is no mouse grabber item. Hover events are delivered directly to items; there is no convenience function for them.

Reimplemented from QObject.

See also contextMenuEvent(), keyPressEvent(), keyReleaseEvent(), mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), focusInEvent(), and focusOutEvent().

### void QGraphicsScene::focusInEvent ( QFocusEvent * focusEvent ) `[virtual protected]`

This event handler, for event focusEvent, can be reimplemented in a subclass to receive focus in events.

The default implementation sets focus on the scene, and then on the last focus item.

See also QGraphicsItem::focusOutEvent().

### QGraphicsItem * QGraphicsScene::focusItem () const

Returns the scene's current focus item, or 0 if no item currently has focus.

The focus item receives keyboard input when the scene receives a key event.

See also setFocusItem() and QGraphicsItem::hasFocus().

### void QGraphicsScene::focusOutEvent ( QFocusEvent * focusEvent ) `[virtual protected]`

This event handler, for event focusEvent, can be reimplemented in a subclass to receive focus out events.

The default implementation removes focus from any focus item, then removes focus from the scene.

See also QGraphicsItem::focusInEvent().

### bool QGraphicsScene::hasFocus () const

Returns true if the scene has focus; otherwise returns false. If the scene has focus, it will will forward key events from QKeyEvent to any item that has focus.

See also setFocus() and setFocusItem().

### qreal QGraphicsScene::height () const

This convenience function is equivalent to calling `sceneRect().height()`.

See also width().

### void QGraphicsScene::helpEvent ( QGraphicsSceneHelpEvent * helpEvent ) `[virtual protected]`

This event handler, for event helpEvent, can be reimplemented in a subclass to receive help events. The events are of type QEvent::ToolTip, which are created when a tooltip is requested.

The default implementation shows the tooltip of the topmost item, i.e., the item with the highest z-value, at the mouse cursor position. If no item has a tooltip set, this function does nothing.

See also QGraphicsItem::toolTip() and QGraphicsSceneHelpEvent.

### void QGraphicsScene::inputMethodEvent ( QInputMethodEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented in a subclass to receive input method events for the scene.

The default implementation forwards the event to the focusItem(). If no item currently has focus, this function does nothing.

See also QGraphicsItem::inputMethodEvent().

### QVariant QGraphicsScene::inputMethodQuery ( Qt::InputMethodQuery query ) const `[virtual]`

This method is used by input methods to query a set of properties of the scene to be able to support complex input method operations as support for surrounding text and reconversions.

The query parameter specifies which property is queried.

See also QWidget::inputMethodQuery().

### QGraphicsItem * QGraphicsScene::itemAt ( const QPointF & position ) const

Returns the topmost visible item at the specified position, or 0 if there are no items at this position.

See also items() and collidingItems().

### QGraphicsItem * QGraphicsScene::itemAt ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

Returns the topmost item at the position specified by (x, y), or 0 if there are no items at this position.

This convenience function is equivalent to calling `itemAt(QPointF(x, y))`.

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

Returns a list of all items on the scene, in no particular order.

See also addItem() and removeItem().

### QList<QGraphicsItem *> QGraphicsScene::items ( const QPointF & pos ) const

This is an overloaded member function, provided for convenience.

Returns all visible items at position pos in the scene. 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).

See also itemAt().

### QList<QGraphicsItem *> QGraphicsScene::items ( const QRectF & rectangle, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const

This is an overloaded member function, provided for convenience.

Returns all visible items that, depending on mode, are either inside or intersect with the specified rectangle.

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

See also itemAt().

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

This is an overloaded member function, provided for convenience.

Returns all visible items that, depending on mode, are either inside or intersect with the polygon polygon.

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

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

This is an overloaded member function, provided for convenience.

Returns all visible items that, depending on path, are either inside or intersect with the path path.

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

### QRectF QGraphicsScene::itemsBoundingRect () const

Calculates and returns the bounding rect of all items on the scene. This function works by iterating over all items, and because if this, it can be slow for large scenes.

See also sceneRect().

### void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent ) `[virtual protected]`

This event handler, for event keyEvent, can be reimplemented in a subclass to receive keypress events. The default implementation forwards the event to current focus item.

See also QGraphicsItem::keyPressEvent() and focusItem().

### void QGraphicsScene::keyReleaseEvent ( QKeyEvent * keyEvent ) `[virtual protected]`

This event handler, for event keyEvent, can be reimplemented in a subclass to receive key release events. The default implementation forwards the event to current focus item.

See also QGraphicsItem::keyReleaseEvent() and focusItem().

### void QGraphicsScene::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * mouseEvent ) `[virtual protected]`

This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse doubleclick events for the scene.

If someone doubleclicks on the scene, the scene will first receive a mouse press event, followed by a release event (i.e., a click), then a doubleclick event, and finally a release event. If the doubleclick event is delivered to a different item than the one that received the first press and release, it will be delivered as a press event. However, tripleclick events are not delivered as doubleclick events in this case.

The default implementation is similar to mousePressEvent().

See also QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseMoveEvent(), QGraphicsItem::mouseReleaseEvent(), and QGraphicsItem::setAcceptedMouseButtons().

### QGraphicsItem * QGraphicsScene::mouseGrabberItem () const

Returns the current mouse grabber item, or 0 if no item is currently grabbing the mouse. The mouse grabber item is the item that receives all mouse events sent to the scene.

An item becomes a mouse grabber when it receives and accepts a mouse press event, and it stays the mouse grabber until either of the following events occur:

- If the item receives a mouse release event when there are no other buttons pressed, it loses the mouse grab.
- If the item becomes invisible (i.e., someone calls `item->setVisible(false))`, or if it becomes disabled (i.e., someone calls `item->setEnabled(false))`, it loses the mouse grab.
- If the item is removed from the scene, it loses the mouse grab.

If the item loses its mouse grab, the scene will ignore all mouse events until a new item grabs the mouse (i.e., until a new item receives a mouse press event).

### void QGraphicsScene::mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent ) `[virtual protected]`

This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse move events for the scene.

The default implementation depends on the mouse grabber state. If there is a mouse grabber item, the event is sent to the mouse grabber; otherwise, the event is ignored.

See also QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseReleaseEvent(), QGraphicsItem::mouseDoubleClickEvent(), and QGraphicsItem::setAcceptedMouseButtons().

### void QGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent ) `[virtual protected]`

This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse press events for the scene.

The default implementation depends on the state of the scene. If there is a mouse grabber item, then the event is sent to the mouse grabber. Otherwise, it is forwarded to the topmost item that accepts mouse events at the scene position from the event, and that item promptly becomes the mouse grabber item.

If there is no item at the given position on the scene, the selection area is reset, any focus item loses its input focus, and the event is then ignored.

See also QGraphicsItem::mousePressEvent() and QGraphicsItem::setAcceptedMouseButtons().

### void QGraphicsScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent ) `[virtual protected]`

This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse release events for the scene.

The default implementation depends on the mouse grabber state. If there is no mouse grabber, the event is ignored. Otherwise, if there is a mouse grabber item, the event is sent to the mouse grabber. If this mouse release represents the last pressed button on the mouse, the mouse grabber item then loses the mouse grab.

See also QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseMoveEvent(), QGraphicsItem::mouseDoubleClickEvent(), and QGraphicsItem::setAcceptedMouseButtons().

### void QGraphicsScene::removeItem ( QGraphicsItem * item )

Removes the item item and all its children from the scene. The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).

See also addItem().

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

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

     QGraphicsScene scene;
     scene.addItem(...
     ...
     QPrinter printer(QPrinter::HighResolution);
     printer.setPageSize(QPrinter::A4);

     QPainter painter(&printer);
     scene.render(&painter);

If source is a null rect, this function will use sceneRect() to determine what to render. If target is a null rect, the dimensions of painter's paint device 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 QGraphicsView::render().

### void QGraphicsScene::sceneRectChanged ( const QRectF & rect ) `[signal]`

This signal is emitted by QGraphicsScene whenever the scene rect changes. The rect parameter is the new scene rectangle.

See also QGraphicsView::updateSceneRect().

### QList<QGraphicsItem *> QGraphicsScene::selectedItems () const

Returns a list of all currently selected items. The items are returned in no particular order.

See also setSelectionArea().

### void QGraphicsScene::setFocus ( Qt::FocusReason focusReason = Qt::OtherFocusReason )

Sets focus on the scene by sending a QFocusEvent to the scene, passing focusReason as the reason. If the scene regains focus after having previously lost it while an item had focus, the last focus item will receive focus with focusReason as the reason.

If the scene already has focus, this function does nothing.

See also hasFocus(), clearFocus(), and setFocusItem().

### void QGraphicsScene::setFocusItem ( QGraphicsItem * item, Qt::FocusReason focusReason = Qt::OtherFocusReason )

Sets the scene's focus item to item, with the focus reason focusReason, after removing focus from any previous item that may have had focus.

If item is 0, or if it either does not accept focus (i.e., it does not have the QGraphicsItem::ItemIsFocusable flag enabled), or is not visible or not enabled, this function only removes focus from any previous focusitem.

If item is not 0, and the scene does not currently have focus (i.e., hasFocus() returns false), this function will call setFocus() automatically.

See also focusItem(), hasFocus(), and setFocus().

### void QGraphicsScene::setSelectionArea ( const QPainterPath & path )

Sets the selection area to path. All items within this area will be marked as selected. You can get the list of all selected items by calling selectedItems().

For an item to be selected, it must be marked as selectable (QGraphicsItem::ItemIsSelectable).

See also clearSelection().

### void QGraphicsScene::update ( const QRectF & rect = QRectF() ) `[slot]`

Schedules a redraw of the area rect on the scene.

See also sceneRect() and changed().

### QList<QGraphicsView *> QGraphicsScene::views () const

Returns a list of all the views that display this scene.

See also QGraphicsView::scene().

### void QGraphicsScene::wheelEvent ( QGraphicsSceneWheelEvent * wheelEvent ) `[virtual protected]`

This event handler, for event wheelEvent, can be reimplemented in a subclass to receive mouse wheel events for the scene.

By default, the event is delivered to the topmost visible item under the cursor. If ignored, the event propagates to the item beneath, and again until the event is accepted, or it reaches the scene. If no items accept the event, it is ignored.

See also QGraphicsItem::wheelEvent().

### qreal QGraphicsScene::width () const

This convenience function is equivalent to calling sceneRect().width().

See also height().