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

# QGraphicsItem Class Reference
[QtGui module]

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene. More...

     #include <QGraphicsItem>

Inherited by QAbstractGraphicsShapeItem, QGraphicsItemGroup, QGraphicsLineItem, QGraphicsPixmapItem, QGraphicsSvgItem, and QGraphicsTextItem.

This class was introduced in Qt 4.2.

- List of all members, including inherited members

### Public Types

- enum GraphicsItemChange { ItemEnabledChange, ItemMatrixChange, ItemPositionChange, ItemSelectedChange, ..., ItemChildRemovedChange }
- enum GraphicsItemFlag { ItemIsMovable, ItemIsSelectable, ItemIsFocusable }
- flags GraphicsItemFlags

### Public Functions

- QGraphicsItem ( QGraphicsItem * parent = 0, QGraphicsScene * scene = 0 )
- virtual ~QGraphicsItem ()
- bool acceptDrops () const
- Qt::MouseButtons acceptedMouseButtons () const
- bool acceptsHoverEvents () const
- virtual void advance ( int phase )
- virtual QRectF boundingRect () const = 0
- QList<QGraphicsItem *> children () const
- QRectF childrenBoundingRect () const
- void clearFocus ()
- virtual bool collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- virtual bool collidesWithPath ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- QList<QGraphicsItem *> collidingItems ( Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
- virtual bool contains ( const QPointF & point ) const
- QCursor cursor () const
- QVariant data ( int key ) const
- void ensureVisible ( const QRectF & rect = QRectF(), int xmargin = 50, int ymargin = 50 )
- void ensureVisible ( qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50 )
- GraphicsItemFlags flags () const
- QGraphicsItemGroup * group () const
- bool handlesChildEvents () const
- bool hasCursor () const
- bool hasFocus () const
- void hide ()
- void installSceneEventFilter ( QGraphicsItem * filterItem )
- bool isAncestorOf ( const QGraphicsItem * child ) const
- bool isEnabled () const
- bool isObscured () const
- virtual bool isObscuredBy ( const QGraphicsItem * item ) const
- bool isSelected () const
- bool isVisible () const
- QPointF mapFromItem ( const QGraphicsItem * item, const QPointF & point ) const
- QPolygonF mapFromItem ( const QGraphicsItem * item, const QRectF & rect ) const
- QPolygonF mapFromItem ( const QGraphicsItem * item, const QPolygonF & polygon ) const
- QPainterPath mapFromItem ( const QGraphicsItem * item, const QPainterPath & path ) const
- QPointF mapFromItem ( const QGraphicsItem * item, qreal x, qreal y ) const
- QPointF mapFromParent ( const QPointF & point ) const
- QPolygonF mapFromParent ( const QRectF & rect ) const
- QPolygonF mapFromParent ( const QPolygonF & polygon ) const
- QPainterPath mapFromParent ( const QPainterPath & path ) const
- QPointF mapFromParent ( qreal x, qreal y ) const
- QPointF mapFromScene ( const QPointF & point ) const
- QPolygonF mapFromScene ( const QRectF & rect ) const
- QPolygonF mapFromScene ( const QPolygonF & polygon ) const
- QPainterPath mapFromScene ( const QPainterPath & path ) const
- QPointF mapFromScene ( qreal x, qreal y ) const
- QPointF mapToItem ( const QGraphicsItem * item, const QPointF & point ) const
- QPolygonF mapToItem ( const QGraphicsItem * item, const QRectF & rect ) const
- QPolygonF mapToItem ( const QGraphicsItem * item, const QPolygonF & polygon ) const
- QPainterPath mapToItem ( const QGraphicsItem * item, const QPainterPath & path ) const
- QPointF mapToItem ( const QGraphicsItem * item, qreal x, qreal y ) const
- QPointF mapToParent ( const QPointF & point ) const
- QPolygonF mapToParent ( const QRectF & rect ) const
- QPolygonF mapToParent ( const QPolygonF & polygon ) const
- QPainterPath mapToParent ( const QPainterPath & path ) const
- QPointF mapToParent ( qreal x, qreal y ) const
- QPointF mapToScene ( const QPointF & point ) const
- QPolygonF mapToScene ( const QRectF & rect ) const
- QPolygonF mapToScene ( const QPolygonF & polygon ) const
- QPainterPath mapToScene ( const QPainterPath & path ) const
- QPointF mapToScene ( qreal x, qreal y ) const
- QMatrix matrix () const
- void moveBy ( qreal dx, qreal dy )
- virtual QPainterPath opaqueArea () const
- virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) = 0
- QGraphicsItem * parentItem () const
- QPointF pos () const
- void removeSceneEventFilter ( QGraphicsItem * filterItem )
- void resetMatrix ()
- void rotate ( qreal angle )
- void scale ( qreal sx, qreal sy )
- QGraphicsScene * scene () const
- QRectF sceneBoundingRect () const
- QMatrix sceneMatrix () const
- QPointF scenePos () const
- void setAcceptDrops ( bool on )
- void setAcceptedMouseButtons ( Qt::MouseButtons buttons )
- void setAcceptsHoverEvents ( bool enabled )
- void setCursor ( const QCursor & cursor )
- void setData ( int key, const QVariant & value )
- void setEnabled ( bool enabled )
- void setFlag ( GraphicsItemFlag flag, bool enabled = true )
- void setFlags ( GraphicsItemFlags flags )
- void setFocus ( Qt::FocusReason focusReason = Qt::OtherFocusReason )
- void setGroup ( QGraphicsItemGroup * group )
- void setHandlesChildEvents ( bool enabled )
- void setMatrix ( const QMatrix & matrix, bool combine = false )
- void setParentItem ( QGraphicsItem * parent )
- void setPos ( const QPointF & pos )
- void setPos ( qreal x, qreal y )
- void setSelected ( bool selected )
- void setToolTip ( const QString & toolTip )
- void setVisible ( bool visible )
- void setZValue ( qreal z )
- virtual QPainterPath shape () const
- void shear ( qreal sh, qreal sv )
- void show ()
- QString toolTip () const
- QGraphicsItem * topLevelItem () const
- void translate ( qreal dx, qreal dy )
- virtual int type () const
- void unsetCursor ()
- void update ( const QRectF & rect = QRectF() )
- void update ( qreal x, qreal y, qreal width, qreal height )
- qreal x () const
- qreal y () const
- qreal zValue () const

### Static Public Members

- const int UserType

### Protected Functions

- virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event )
- virtual void dragEnterEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void dragMoveEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void dropEvent ( QGraphicsSceneDragDropEvent * event )
- virtual void focusInEvent ( QFocusEvent * event )
- virtual void focusOutEvent ( QFocusEvent * event )
- virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
- virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
- virtual void hoverMoveEvent ( QGraphicsSceneHoverEvent * event )
- virtual void inputMethodEvent ( QInputMethodEvent * event )
- virtual QVariant inputMethodQuery ( Qt::InputMethodQuery query ) const
- virtual QVariant itemChange ( GraphicsItemChange change, const QVariant & value )
- virtual void keyPressEvent ( QKeyEvent * event )
- virtual void keyReleaseEvent ( QKeyEvent * event )
- virtual void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
- virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
- virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event )
- virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
- void prepareGeometryChange ()
- virtual bool sceneEvent ( QEvent * event )
- virtual bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event )
- virtual void wheelEvent ( QGraphicsSceneWheelEvent * event )

### Related Non-Members

- T qgraphicsitem_cast ( QGraphicsItem * item )

## Detailed Description

The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.

It provides a light-weight foundation for writing your own custom items. This includes defining the item's geometry, collision detection, its painting implementation and item interaction through its event handlers. QGraphicsItem is part of The Graphics View Framework

For convenience, Qt provides a set of standard graphics items for the most common shapes. These are:

- QGraphicsEllipseItem provides an ellipse item
- QGraphicsLineItem provides a line item
- QGraphicsPathItem provides an arbitrary path item
- QGraphicsPixmapItem provides a pixmap item
- QGraphicsPolygonItem provides a polygon item
- QGraphicsRectItem provides a rectangular item
- QGraphicsTextItem provides a text item

All of an item's geometric information is based on its local coordinate system. The item's position, pos(), is the only function that does not operate in local coordinates, as it returns a position in parent coordinates. {The Graphics View Coordinate System} describes the coordinate system in detail.

You can set whether an item should be visible (i.e., drawn, and accepting events), by calling setVisible(). Hiding an item will also hide its children. Similarily, you can enable or disable an item by calling setEnabled(). If you disable an item, all its children will also be disabled. By default, items are both visible and enabled. To toggle whether an item is selected or not, first enable selection by setting the ItemIsSelectable flag, and then call setSelected(). Normally, selection is toggled by the scene, as a result of user interaction.

To write your own graphics item, you first create a subclass of QGraphicsItem, and then start by implementing its two pure virtual public functions: boundingRect(), which returns an estimate of the area painted by the item, and paint(), which implements the actual painting. For example:

     class SimpleItem : public QGraphicsItem
     {
     public:
         QRectF boundingRect() const
         {
             qreal penWidth = 1;
             return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                           20 + penWidth / 2, 20 + penWidth / 2);
         }

         void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                    QWidget *widget)
         {
             painter->drawRoundRect(-10, -10, 20, 20);
         }
     };

The boundingRect() function has many different purposes. QGraphicsScene bases its item index on boundingRect(), and QGraphicsView uses it both for culling invisible items, and for determining the area that needs to be recomposed when drawing overlapping items. In addition, QGraphicsItem's collision detection mechanisms use boundingRect() to provide an efficient cut-off. The fine grained collision algorithm in collidesWithItem() is based on calling shape(), which returns an accurate outline of the item's shape as a QPainterPath.

Collision detection can be done in two ways:

1. Reimplement shape() to return an accurate shape for your item, and rely on the default implementation of collidesWithItem() to do shape-shape intersection. This can be rather expensive if the shapes are complex.
2. Reimplement collidesWithItem() to provide your own custom item and shape collision algorithm.

The contains() function can be called to determine whether the item contains a point or not. This function can also be reimplemented by the item. The default behavior of contains() is based on calling shape().

Items can contain other items, and also be contained by other items. All items can have a parent item and a list of children. Unless the item has no parent, its position is in parent coordinates (i.e., the parent's local coordinates). Parent items propagate both their position and their transformation to all children.

QGraphicsItem supports affine transformations in addition to its base position, pos(). To change the item's transformation, you can either pass a transformation matrix to setMatrix(), or call one of the convenience functions rotate(), scale(), translate(), or shear(). Item transformations accumulate from parent to child, so if both a parent and child item are rotated 90 degrees, the child's total transformation will be 180 degrees. Similarily, if the item's parent is scaled to 2x its original size, its children will also be twice as large. An item's transformation does not affect its own local geometry; all geometry functions (e.g., contains(), update(), and all the mapping functions) still operate in local coordinates. For convenience, QGraphicsItem provides the functions sceneMatrix(), which returns the item's total transformation matrix (including its position and all parents' positions and transformations), and scenePos(), which returns its position in scene coordinates. To reset an item's matrix, call resetMatrix().

The paint() function is called by QGraphicsView to paint the item's contents. The item has no background or default fill of its own; whatever is behind the item will shine through all areas that are not explicitly painted in this function. You can call update() to schedule a repaint, optionally passing the rectangle that needs a repaint. Depending on whether or not the item is visible in a view, the item may or may not be repainted; there is no equivalent to QWidget::repaint() in QGraphicsItem.

Items are painted by the view, starting with the parent items and then drawing children, in ascending stacking order. You can set an item's stacking order by calling setZValue(), and test it by calling zValue(). Stacking order applies to sibling items; parents are always drawn before their children.

QGraphicsItem receives events from QGraphicsScene through the virtual function sceneEvent(). This function distributes the most common events to a set of convenience event handlers:

- contextMenuEvent() handles context menu events
- focusInEvent() and focusOutEvent() handle focus in and out events
- hoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent() handles hover enter, move and leave events
- inputMethodEvent() handles input events, for accessibility support
- keyPressEvent() and keyReleaseEvent handle key press and release events
- mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and mouseDoubleClickEvent() handles mouse press, move, release, click and doubleclick events

You can filter events for any other item by installing event filters. This functionaly is separate from from Qt's regular event filters (see QObject::installEventFilter()), which only work on subclasses of QObject. After installing your item as an event filter for another item by calling installSceneEventFilter(), the filtered events will be received by the virtual function sceneEventFilter(). You can remove item event filters by calling removeSceneEventFilter().

Sometimes it's useful to register custom data with an item, be it a custom item, or a standard item. You can call setData() on any item to store data in it using a key-value pair (the key being an integer, and the value is a QVariant). To get custom data from an item, call data(). This functionality is completely untouched by Qt itself; it is provided for the user's convenience.

See also QGraphicsScene, QGraphicsView, and The Graphics View Framework.

## Member Type Documentation

### enum QGraphicsItem::GraphicsItemChange

This enum describes the state changes that are notified by QGraphicsItem::itemChange(). The notifications are sent as the state changes, and in some cases, adjustments can be made (see the documentation for each change for details).

Note: Be careful with calling functions on the QGraphicsItem itself inside itemChange(), as certain function calls can lead to unwanted recursion. For example, you cannot call setPos() in itemChange() on an ItemPositionChange notification, as the setPos() function will again call itemChange(ItemPositionChange). Instead, you can return the new, adjusted position from itemChange().

Constant | Value | Description
--- | --- | ---
`QGraphicsItem::ItemEnabledChange` | `3` | The item's enabled state changes. If the item is presently enabled, it will become disabled, and vice verca. The value argument is the new enabled state (i.e., true or false). Do not call setEnabled() in itemChange() as this notification is delivered. Instead, you can return the new state from itemChange().
`QGraphicsItem::ItemMatrixChange` | `1` | The item's matrix changes. This notification is only sent when the item's local matrix changes (i.e., as a result of calling setMatrix(), or one of the convenience transformation functions, such as rotate()). The value argument is the new matrix (i.e., a QMatrix); to get the old matrix, call matrix(). Do not call setMatrix() or any of the transformation convenience functions in itemChange() as this notification is delivered; instead, you can return the new matrix from itemChange().
`QGraphicsItem::ItemPositionChange` | `0` | The item's position changes. This notification is only sent when the item's local position changes, relative to its parent, has changed (i.e., as a result of calling setPos() or moveBy()). The value argument is the new position (i.e., a QPointF). You can call pos() to get the original position. Do not call setPos() or moveBy() in itemChange() as this notification is delivered; instead, you can return the new, adjusted position from itemChange().
`QGraphicsItem::ItemSelectedChange` | `4` | The item's selected state changes. If the item is presently selected, it will become unselected, and vice verca. The value argument is the new selected state (i.e., true or false). Do not call setSelected() in itemChange() as this notification is delivered(); instead, you can return the new selected state from itemChange().
`QGraphicsItem::ItemVisibleChange` | `2` | The item's visible state changes. If the item is presently visible, it will become invisible, and vice verca. The value argument is the new visible state (i.e., true or false). Do not call setVisible() in itemChange() as this notification is delivered; instead, you can return the new visible state from itemChange().
`QGraphicsItem::ItemParentChange` | `5` | The item's parent changes. The value argument is the new parent item (i.e., a QGraphicsItem pointer). Do not call setParentItem() in itemChange() as this notification is delivered; instead, you can return the new parent from itemChange().
`QGraphicsItem::ItemChildAddedChange` | `6` | A child is added to this item. The value argument is the new child item (i.e., a QGraphicsItem pointer). Do not pass this item to any item's setParentItem() function as this notification is delivered. The return value is unused; you cannot adjust anything in this notification.
`QGraphicsItem::ItemChildRemovedChange` | `7` | A child is removed from this item. The value argument is the child item that is about to be removed (i.e., a QGraphicsItem pointer). The return value is unused; you cannot adjust anything in this notification.

### enum QGraphicsItem::GraphicsItemFlag
flags QGraphicsItem::GraphicsItemFlags

This enum describes different flags that you can set on an item to toggle different features in the item's behavior.

All flags are disabled by default.

Constant | Value | Description
--- | --- | ---
`QGraphicsItem::ItemIsMovable` | `0x1` | The item supports interactive movement using the mouse. By clicking on the item and then dragging, the item will move together with the mouse cursor. If the item has children, all children are also moved. If the item is part of a selection, all selected items are also moved. This feature is provided as a convenience through the base implementation of QGraphicsItem's mouse event handlers.
`QGraphicsItem::ItemIsSelectable` | `0x2` | The item supports selection. Enabling this feature will enable setSelected() to toggle selection for the item. It will also let the item be selected automatically as a result of calling QGraphicsScene::setSelectionArea(), by clicking on an item, or by using rubber band selection in QGraphicsView.
`QGraphicsItem::ItemIsFocusable` | `0x4` | The item supports keyboard input focus (i.e., it is an input item). Enabling this flag will allow the item to accept focus, which again allows the delivery of key events to QGraphicsItem::keyPressEvent() and QGraphicsItem::keyReleaseEvent().

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

## Member Function Documentation

### QGraphicsItem::QGraphicsItem ( QGraphicsItem * parent = 0, QGraphicsScene * scene = 0 )

Constructs a QGraphicsItem with the parent parent on scene. If parent is 0, the item will be a top-level. If scene is 0, the item will not be associated with a scene.

See also QGraphicsScene::addItem().

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

Destroys the QGraphicsItem and all its children. If this item is currently associated with a scene, the item will be removed from the scene before it is deleted.

### bool QGraphicsItem::acceptDrops () const

Returns true if this item can accept drag and drop events; otherwise, returns false. By default, items do not accept drag and drop events; items are transparent to drag and drop.

See also setAcceptDrops().

### Qt::MouseButtons QGraphicsItem::acceptedMouseButtons () const

Returns the mouse buttons that this item accepts mouse events for. By default, all mouse buttons are accepted.

If an item accepts a mouse button, it will become the mouse grabber item when a mouse press event is delivered for that mouse button. However, if the item does not accept the button, QGraphicsScene will forward the mouse events to the first item beneath it that does.

See also setAcceptedMouseButtons() and mousePressEvent().

### bool QGraphicsItem::acceptsHoverEvents () const

Returns true if an item accepts hover events (QGraphicsSceneHoverEvent); otherwise, returns false. By default, items do not accept hover events.

See also setAcceptsHoverEvents() and setAcceptedMouseButtons().

### void QGraphicsItem::advance ( int phase ) `[virtual]`

This virtual function is called twice for all items by the QGraphicsScene::advance() slot. In the first phase, all items are called with phase == 0, indicating that items on the scene are about to advance, and then all items are called with phase == 1. Reimplement this function to update your item if you need simple scene-controlled animation.

The default implementation does nothing.

For individual item animation, an alternative to this function is to either use QGraphicsItemAnimation, or to multiple-inherit from QObject and QGraphicsItem, and animate your item using QObject::startTimer() and QObject::timerEvent().

See also QGraphicsItemAnimation and QTimeLine.

### QRectF QGraphicsItem::boundingRect () const `[pure virtual]`

This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.

Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation (scale(), rotate(), etc.).

Reimplement this function to let QGraphicsView determine what parts of the widget, if any, need to be redrawn.

Note: For shapes that paint an outline / stroke, it is important to include half the pen width in the bounding rect. It is not necessary to compensate for antialiasing, though.

Example:

     QRectF CircleItem::boundingRect() const
     {
         qreal penWidth = 1;
         return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
                       diameter + penWidth, diameter + penWidth);
     }

See also shape(), contains(), and The Graphics View Coordinate System.

### QList<QGraphicsItem *> QGraphicsItem::children () const

Returns a list of this item's children. The items are returned in no particular order.

See also setParentItem().

### QRectF QGraphicsItem::childrenBoundingRect () const

Returns the bounding rect of this item's descendents (i.e., its children, their children, etc.) in local coordinates. If the item has no children, this function returns an empty QRectF.

This does not include this item's own bounding rect; it only returns its descendents' accumulated bounding rect. If you need to include this item's bounding rect, you can add boundingRect() to childrenBoundingRect() using QRectF::operator|().

This function is linear in complexity; it determines the size of the returned bounding rect by iterating through all descendents.

See also boundingRect() and sceneBoundingRect().

### void QGraphicsItem::clearFocus ()

Takes keyboard input focus from the item.

If it has focus, a focus out event is sent to this item to tell it that it is about to lose the focus.

Only items that set the ItemIsFocusable flag can accept keyboard focus.

See also setFocus().

### bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const `[virtual]`

Returns true if this item collides with other; otherwise returns false. The ways items collide is determined by mode. The default value for mode is Qt::IntersectsItemShape; other collides with this item if it either intersect or are contained by this item's shape.

The default implementation is based on shape intersection, and it calls shape() on both items. Because the complexity of arbitrary shape-shape intersection grows with an order of magnitude when the shapes are complex, this operation can be noticably time consuming. You have the option of reimplementing this function in a subclass of QGraphicsItem to provide a custom algorithm. This allows you to make use of natural constraints in the shapes of your own items, in order to improve the performance of the collision detection. For instance, two untransformed perfectly circular items' collision can be determined very efficiently by comparing their positions and radii.

Keep in mind that when reimplementing this function and calling shape() or boundingRect() on other, the returned coordinates must be mapped to this item's coordinate system before any intersection can take place.

See also contains() and shape().

### bool QGraphicsItem::collidesWithPath ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const `[virtual]`

Returns true if this item collides with path.

The collision is determined by mode. The default value for mode is Qt::IntersectsItemShape; path collides with this item if it either intersects or is contained by this item's shape.

See also collidesWithItem(), contains(), and shape().

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

Returns a list of all items that collide with this item.

The way collisions are detected is determined by mode. The default value for mode is Qt::IntersectsItemShape; All items whose shape intersects or is contained by this item's shape are returned.

See also QGraphicsScene::collidingItems() and collidesWithItem().

### bool QGraphicsItem::contains ( const QPointF & point ) const `[virtual]`

Returns true if this item contains point, which is in local coordinates; otherwise, false is returned. It is most often called from QGraphicsView to determine what item is under the cursor, and for that reason, the implementation of this function should be as light-weight as possible.

By default, this function calls shape(), but you can reimplement it in a subclass to provide a (perhaps more efficient) implementation.

See also shape(), boundingRect(), and collidesWithPath().

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

This event handler, for event event, can be reimplemented to receive context menu events for this item.

If you ignore the event, (i.e., by calling QEvent::ignore(),) event will propagate to any item beneath this item. If no items accept the event, it will be ignored by the scene, and propagate to the view.

The default implementation does nothing.

See also sceneEvent().

### QCursor QGraphicsItem::cursor () const

Returns the current cursor shape for the item. The mouse cursor will assume this shape when it's over this item. See the list of predefined cursor objects for a range of useful shapes.

An editor item might want to use an I-beam cursor:

     item->setCursor(Qt::IBeamCursor);

If no cursor has been set, the parent's cursor is used.

See also setCursor(), hasCursor(), unsetCursor(), QWidget::cursor, and QApplication::overrideCursor().

### QVariant QGraphicsItem::data ( int key ) const

Returns this item's custom data for the key key as a QVariant.

Custom item data is useful for storing arbitrary properties in any item. Example:

     static const int ObjectName = 0;

     QGraphicsItem *item = scene.itemAt(100, 50);
     if (item->data(ObjectName).toString().isEmpty()) {
         if (qgraphicsitem_cast<ButtonItem *>(item))
             item->setData(ObjectName, "Button");
     }

Qt does not use this feature for storing data; it is provided solely for the convenience of the user.

See also setData().

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

This event handler, for event event, can be reimplemented to receive drag enter events for this item. Drag enter events are generated as the cursor enters the item's area.

By accepting the event, (i.e., by calling QEvent::accept(),) the item will accept drop events, in addition to receiving drag move and drag leave. Otherwise, the event will be ignored and propagate to the item beneath. If the event is accepted, the item will receive a drag move event before control goes back to the event loop.

A common implementation of dragEnterEvent accepts or ignores event depending on the associated mime data in event. Example:

     CustomItem::CustomItem()
     {
         setAcceptDrops(true);
         ...
     }

     void CustomItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
     {
         event->setAccepted(event->mimeData()->hasFormat("text/plain"));
     }

Items do not receive drag and drop events by default; to enable this feature, call `setAcceptDrops(true)`.

The default implementation does nothing.

See also dropEvent(), dragMoveEvent(), and dragLeaveEvent().

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

This event handler, for event event, can be reimplemented to receive drag leave events for this item. Drag leave events are generated as the cursor leaves the item's area. Most often you will not need to reimplement this function, but it can be useful for resetting state in your item (e.g., highlighting).

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

Items do not receive drag and drop events by default; to enable this feature, call `setAcceptDrops(true)`.

The default implementation does nothing.

See also dragEnterEvent(), dropEvent(), and dragMoveEvent().

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

This event handler, for event event, can be reimplemented to receive drag move events for this item. Drag move events are generated as the cursor moves around inside the item's area. Most often you will not need to reimplement this function; it is used to indicate that only parts of the item can accept drops.

Calling QEvent::ignore() or QEvent::accept() on event toggles whether or not the item will accept drops at the position from the event. By default, event is accepted, indicating that the item allows drops at the specified position.

Items do not receive drag and drop events by default; to enable this feature, call `setAcceptDrops(true)`.

The default implementation does nothing.

See also dropEvent(), dragEnterEvent(), and dragLeaveEvent().

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

This event handler, for event event, can be reimplemented to receive drop events for this item. Items can only receive drop events if the last drag move event was accepted.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

Items do not receive drag and drop events by default; to enable this feature, call `setAcceptDrops(true)`.

The default implementation does nothing.

See also dragEnterEvent(), dragMoveEvent(), and dragLeaveEvent().

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

If this item is part of a scene that is viewed by a QGraphicsView, this convenience function will attempt to scroll the view to ensure that rect is visible inside the view's viewport. If rect is a null rect (the default), QGraphicsItem will default to the item's bounding rect. xmargin and ymargin are the number of pixels the view should use for margins.

If the specified rect cannot be reached, the contents are scrolled to the nearest valid position.

If this item is not viewed by a QGraphicsView, this function does nothing.

See also QGraphicsView::ensureVisible().

### void QGraphicsItem::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 convenience function is equivalent to calling ensureVisible(QRectF(x, y, w, h), xmargin, ymargin):

### GraphicsItemFlags QGraphicsItem::flags () const

Returns this item's flags. The flags describe what configurable features of the item are enabled and not. For example, if the flags include ItemIsFocusable, the item can accept input focus.

By default, no flags are enabled.

See also setFlags() and setFlag().

### void QGraphicsItem::focusInEvent ( QFocusEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive focus in events for this item. The default implementation does nothing.

See also focusOutEvent() and sceneEvent().

### void QGraphicsItem::focusOutEvent ( QFocusEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive focus out events for this item. The default implementation does nothing.

See also focusInEvent() and sceneEvent().

### QGraphicsItemGroup * QGraphicsItem::group () const

Returns a pointer to this item's item group, or 0 if this item is not member of a group.

See also setGroup(), QGraphicsItemGroup, and QGraphicsScene::createItemGroup().

### bool QGraphicsItem::handlesChildEvents () const

Returns true if this item handles child events (i.e., all events intented for any of its children are instead sent to this item); otherwise, false is returned.

This property is useful for item groups; it allows one item to handle events on behalf of its children, as opposed to its children handling their events individually.

The default is to return false; children handle their own events.

See also setHandlesChildEvents().

### bool QGraphicsItem::hasCursor () const

Returns true if this item has a cursor set; otherwise, false is returned.

By default, items don't have any cursor set. cursor() will return a standard pointing arrow cursor.

See also unsetCursor().

### bool QGraphicsItem::hasFocus () const

Returns true if this item has focus (i.e., can accept key events); otherwise, returns false.

See also setFocus() and QGraphicsScene::setFocusItem().

### void QGraphicsItem::hide ()

Hides the item. (Items are visible by default.)

This convenience function is equivalent to calling `setVisible(false)`.

See also show() and setVisible().

### void QGraphicsItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive hover enter events for this item. The default implementation calls update(); otherwise it does nothing.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

See also hoverMoveEvent(), hoverLeaveEvent(), and sceneEvent().

### void QGraphicsItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive hover leave events for this item. The default implementation calls update(); otherwise it does nothing.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

See also hoverEnterEvent(), hoverMoveEvent(), and sceneEvent().

### void QGraphicsItem::hoverMoveEvent ( QGraphicsSceneHoverEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive hover move events for this item. The default implementation does nothing.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

See also hoverEnterEvent(), hoverLeaveEvent(), and sceneEvent().

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

This event handler, for event event, can be reimplemented to receive input method events for this item. The default implementation ignores the event.

See also inputMethodQuery() and sceneEvent().

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

This method is only relevant for input items. It is used by the input method to query a set of properties of the item to be able to support complex input method operations, such as support for surrounding text and reconversions. query specifies which property is queried.

See also inputMethodEvent().

### void QGraphicsItem::installSceneEventFilter ( QGraphicsItem * filterItem )

Installs an event filter for this item on filterItem, causing all events for this item to first pass through filterItem's sceneEventFilter() function.

To filter another item's events, install this item as an event filter for the other item. Example:

     QGraphicsScene scene;
     QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
     QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));

     line->installSceneEventFilter(ellipse);
     // line's events are filtered by ellipse's sceneEventFilter() function.

     ellipse->installSceneEventFilter(line);
     // ellipse's events are filtered by line's sceneEventFilter() function.

An item can only filter events for other items in the same scene. Also, an item cannot filter its own events; instead, you can reimplement sceneEvent() directly.

See also removeSceneEventFilter(), sceneEventFilter(), and sceneEvent().

### bool QGraphicsItem::isAncestorOf ( const QGraphicsItem * child ) const

Returns true if this item is an ancestor of child (i.e., if this item is child's parent, or one of child's parent's ancestors).

See also parentItem().

### bool QGraphicsItem::isEnabled () const

Returns true if the item is enabled; otherwise, false is returned.

See also setEnabled().

### bool QGraphicsItem::isObscured () const

Returns true if this item's bounding rect is completely obscured by the opaque shape of any of its colliding items. This function returns false if all the colliding items are at the same zValue() as this item.

See also opaqueArea().

### bool QGraphicsItem::isObscuredBy ( const QGraphicsItem * item ) const `[virtual]`

Returns true if this item's bounding rect is completely obscured by the opaque shape of item.

See also opaqueArea() and isObscured().

### bool QGraphicsItem::isSelected () const

Returns true if this item is selected; otherwise, false is returned.

Items that are in a group inherit the group's selected state.

Items are not selected by default.

See also setSelected() and QGraphicsScene::setSelectionArea().

### bool QGraphicsItem::isVisible () const

Returns true if the item is visible; otherwise, false is returned.

Note that the item's general visibility is unrelated to whether or not it is actually being visualized by a QGraphicsView.

See also setVisible().

### QVariant QGraphicsItem::itemChange ( GraphicsItemChange change, const QVariant & value ) `[virtual protected]`

This virtual function is called by QGraphicsItem to notify custom items that some part of the item's state changes. By reimplementing this function, your can react to a change, and in some cases, (depending on change,) adjustments can be made.

change is the parameter of the item that is changing. value is the new value; the type of the value depends on change.

Example:

     QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
     {
         if (change == ItemPositionChange && scene()) {
             // value is the new position.
             QPointF newPos = value.toPointF();
             QRectF rect = scene()->sceneRect();
             if (!rect.contains(newPos)) {
                 // Keep the item inside the scene rect.
                 newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
                 newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
                 return newPos;
             }
         }
         return QGraphicsItem::itemChange(change, value);
     }

The default implementation does nothing, and returns value.

Note: Certain QGraphicsItem functions cannot be called in a reimplementation of this function; see the GraphicsItemChange documentation for details.

See also GraphicsItemChange.

### void QGraphicsItem::keyPressEvent ( QKeyEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive key press events for this item. The default implementation ignores the event. If you reimplement this handler, the event will by default be accepted.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

Note that key events are only received for items that set the ItemIsFocusable flag, and that have keyboard input focus.

See also keyReleaseEvent(), setFocus(), QGraphicsScene::setFocusItem(), and sceneEvent().

### void QGraphicsItem::keyReleaseEvent ( QKeyEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive key release events for this item. The default implementation ignores the event. If you reimplement this handler, the event will by default be accepted.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

Note that key events are only received for items that set the ItemIsFocusable flag, and that have keyboard input focus.

See also keyPressEvent(), setFocus(), QGraphicsScene::setFocusItem(), and sceneEvent().

### QPointF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QPointF & point ) const

Maps the point point, which is in item's coordinate system, to this item's coordinate system, and returns the mapped coordinate.

If item is 0, this function returns the same as mapFromScene().

See also mapFromParent(), mapFromScene(), matrix(), mapToItem(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in item's coordinate system, to this item's coordinate system, and returns the mapped rectangle as a polygon.

If item is 0, this function returns the same as mapFromScene()

See also mapToItem(), mapFromParent(), matrix(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in item's coordinate system, to this item's coordinate system, and returns the mapped polygon.

If item is 0, this function returns the same as mapFromScene().

See also mapToItem(), mapFromParent(), matrix(), and The Graphics View Coordinate System.

### QPainterPath QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QPainterPath & path ) const

This is an overloaded member function, provided for convenience.

Maps the path path, which is in item's coordinate system, to this item's coordinate system, and returns the mapped path.

If item is 0, this function returns the same as mapFromScene().

See also mapFromParent(), mapFromScene(), mapToItem(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

This convenience function is equivalent to calling mapFromItem(item, QPointF(x, y)).

### QPointF QGraphicsItem::mapFromParent ( const QPointF & point ) const

Maps the point point, which is in this item's parent's coordinate system, to this item's coordinate system, and returns the mapped coordinate.

See also mapFromItem(), mapFromScene(), matrix(), mapToParent(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromParent ( const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in this item's parent's coordinate system, to this item's coordinate system, and returns the mapped rectangle as a polygon.

See also mapToParent(), mapFromItem(), matrix(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromParent ( const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in this item's parent's coordinate system, to this item's coordinate system, and returns the mapped polygon.

See also mapToParent(), mapToItem(), matrix(), and The Graphics View Coordinate System.

### QPainterPath QGraphicsItem::mapFromParent ( const QPainterPath & path ) const

This is an overloaded member function, provided for convenience.

Maps the path path, which is in this item's parent's coordinate system, to this item's coordinate system, and returns the mapped path.

See also mapFromScene(), mapFromItem(), mapToParent(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapFromParent ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

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

### QPointF QGraphicsItem::mapFromScene ( const QPointF & point ) const

Maps the point point, which is in this item's scene's coordinate system, to this item's coordinate system, and returns the mapped coordinate.

See also mapFromItem(), mapFromParent(), matrix(), mapToScene(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromScene ( const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in this item's scene's coordinate system, to this item's coordinate system, and returns the mapped rectangle as a polygon.

See also mapToScene(), mapFromItem(), matrix(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapFromScene ( const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in this item's scene's coordinate system, to this item's coordinate system, and returns the mapped polygon.

See also mapToScene(), mapFromParent(), matrix(), and The Graphics View Coordinate System.

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

This is an overloaded member function, provided for convenience.

Maps the path path, which is in this item's scene's coordinate system, to this item's coordinate system, and returns the mapped path.

See also mapFromParent(), mapFromItem(), mapToScene(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapFromScene ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

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

### QPointF QGraphicsItem::mapToItem ( const QGraphicsItem * item, const QPointF & point ) const

Maps the point point, which is in this item's coordinate system, to item's coordinate system, and returns the mapped coordinate.

If item is 0, this function returns the same as mapToScene().

See also mapToParent(), mapToScene(), matrix(), mapFromItem(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToItem ( const QGraphicsItem * item, const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in this item's coordinate system, to item's coordinate system, and returns the mapped rectangle as a polygon.

If item is 0, this function returns the same as mapToScene().

See also mapToParent(), mapToScene(), mapFromItem(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToItem ( const QGraphicsItem * item, const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in this item's coordinate system, to item's coordinate system, and returns the mapped polygon.

If item is 0, this function returns the same as mapToScene().

See also mapToParent(), mapToScene(), mapFromItem(), and The Graphics View Coordinate System.

### QPainterPath QGraphicsItem::mapToItem ( const QGraphicsItem * item, const QPainterPath & path ) const

This is an overloaded member function, provided for convenience.

Maps the path path, which is in this item's coordinate system, to item's coordinate system, and returns the mapped path.

If item is 0, this function returns the same as mapToScene().

See also mapToParent(), mapToScene(), mapFromItem(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapToItem ( const QGraphicsItem * item, qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

This convenience function is equivalent to calling mapToItem(item, QPointF(x, y)).

### QPointF QGraphicsItem::mapToParent ( const QPointF & point ) const

Maps the point point, which is in this item's coordinate system, to its parent's coordinate system, and returns the mapped coordinate. If the item has no parent, point will be mapped to the scene's coordinate system.

See also mapToItem(), mapToScene(), matrix(), mapFromParent(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToParent ( const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in this item's coordinate system, to its parent's coordinate system, and returns the mapped rectangle as a polygon. If the item has no parent, rect will be mapped to the scene's coordinate system.

See also mapToScene(), mapToItem(), mapFromParent(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToParent ( const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in this item's coordinate system, to its parent's coordinate system, and returns the mapped polygon. If the item has no parent, polygon will be mapped to the scene's coordinate system.

See also mapToScene(), mapToItem(), mapFromParent(), and The Graphics View Coordinate System.

### QPainterPath QGraphicsItem::mapToParent ( const QPainterPath & path ) const

This is an overloaded member function, provided for convenience.

Maps the path path, which is in this item's coordinate system, to its parent's coordinate system, and returns the mapped path. If the item has no parent, path will be mapped to the scene's coordinate system.

See also mapToScene(), mapToItem(), mapFromParent(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapToParent ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

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

### QPointF QGraphicsItem::mapToScene ( const QPointF & point ) const

Maps the point point, which is in this item's coordinate system, to the scene's coordinate system, and returns the mapped coordinate.

See also mapToItem(), mapToParent(), matrix(), mapFromScene(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToScene ( const QRectF & rect ) const

This is an overloaded member function, provided for convenience.

Maps the rectangle rect, which is in this item's coordinate system, to the scene's coordinate system, and returns the mapped rectangle as a polygon.

See also mapToParent(), mapToItem(), mapFromScene(), and The Graphics View Coordinate System.

### QPolygonF QGraphicsItem::mapToScene ( const QPolygonF & polygon ) const

This is an overloaded member function, provided for convenience.

Maps the polygon polygon, which is in this item's coordinate system, to the scene's coordinate system, and returns the mapped polygon.

See also mapToParent(), mapToItem(), mapFromScene(), and The Graphics View Coordinate System.

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

This is an overloaded member function, provided for convenience.

Maps the path path, which is in this item's coordinate system, to the scene's coordinate system, and returns the mapped path.

See also mapToParent(), mapToItem(), mapFromScene(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::mapToScene ( qreal x, qreal y ) const

This is an overloaded member function, provided for convenience.

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

### QMatrix QGraphicsItem::matrix () const

Returns this item's transformation matrix. If no matrix has been set, the identity matrix is returned.

See also setMatrix() and sceneMatrix().

### void QGraphicsItem::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive mouse doubleclick events for this item.

When doubleclicking an item, the item 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.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

The default implementation calls mousePressEvent(). If you want to keep the base implementation when reimplementing this function, call QGraphicsItem::mouseDoubleClickEvent() in your reimplementation.

See also mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and sceneEvent().

### void QGraphicsItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive mouse move events for this item. If you do receive this event, you can be certain that this item also received a mouse press event, and that this item is the current mouse grabber.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

The default implementation handles basic item interaction, such as selection and moving. If you want to keep the base implementation when reimplementing this function, call QGraphicsItem::mouseMoveEvent() in your reimplementation.

See also mousePressEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), and sceneEvent().

### void QGraphicsItem::mousePressEvent ( QGraphicsSceneMouseEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive mouse press events for this item. Mouse press events are only delivered to items that accept the mouse button that is pressed. By default, an item accepts all mouse buttons, but you can change this by calling setAcceptedMouseButtons().

The mouse press event decides which item should become the mouse grabber (see QGraphicsScene::mouseGrabberItem()). If you do not reimplement this function, the press event will propagate to any topmost item beneath this item, and no other mouse events will be delivered to this item.

If you do reimplement this function, event will by default be accepted (see QEvent::accept()), and this item is then the mouse grabber. This allows the item to receive future move, release and doubleclick events. If you call QEvent::ignore() on event, this item will lose the mouse grab, and event will propagate to any topmost item beneath. No further mouse events will be delivered to this item unless a new mouse press event is received.

The default implementation handles basic item interaction, such as selection and moving. If you want to keep the base implementation when reimplementing this function, call QGraphicsItem::mousePressEvent() in your reimplementation.

See also mouseMoveEvent(), mouseReleaseEvent(), mouseDoubleClickEvent(), and sceneEvent().

### void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) `[virtual protected]`

This event handler, for event event, can be reimplemented to receive mouse release events for this item.

Calling QEvent::ignore() or QEvent::accept() on event has no effect.

The default implementation handles basic item interaction, such as selection and moving. If you want to keep the base implementation when reimplementing this function, call QGraphicsItem::mouseReleaseEvent() in your reimplementation.

See also mousePressEvent(), mouseMoveEvent(), mouseDoubleClickEvent(), and sceneEvent().

### void QGraphicsItem::moveBy ( qreal dx, qreal dy )

Moves the item by dx points horizontally, and dy point vertically. This function is equivalent to calling setPos(pos() + QPointF(dx, dy)).

### QPainterPath QGraphicsItem::opaqueArea () const `[virtual]`

This virtual function returns a shape representing the area where this item is opaque. An area is opaque if it is filled using an opaque brush or color (i.e., not transparent).

This function is used by isObscuredBy(), which is called by underlying items to determine if they are obscured by this item.

The default implementation returns an empty QPainterPath, indicating that this item is completely transparent and does not obscure any other items.

See also isObscuredBy(), isObscured(), and shape().

### void QGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) `[pure virtual]`

This function, which is usually called by QGraphicsView, paints the contents of an item in local coordinates.

Reimplement this function in a QGraphicsItem subclass to provide the item's painting implementation, using painter. The option parameter provides style options for the item, such as its state, exposed area and its level-of-detail hints. The widget argument is optional. If provided, it points to the widget that is being painted on; otherwise, it is 0.

     void RoundRectItem::paint(QPainter *painter,
                               const QStyleOptionGraphicsItem *option,
                               QWidget *widget)
     {
         painter->drawRoundRect(-10, -10, 20, 20);
     }

The painter's pen is 0-width by default, and its pen is initialized to the QPalette::Text brush from the paint device's palette. The brush is initialized to QPalette::Window.

All painting is done in local coordinates.

### QGraphicsItem * QGraphicsItem::parentItem () const

Returns a pointer to this item's parent item. If this item does not have a parent, 0 is returned.

See also setParentItem() and children().

### QPointF QGraphicsItem::pos () const

Returns the position of the item in parent coordinates. If the item has no parent, its position is given in scene coordinates.

The position of the item describes its origin (local coordinate (0, 0)) in parent coordinates; this function returns the same as mapToParent(0, 0).

For convenience, you can also call scenePos() to determine the item's position in scene coordinates, regardless of its parent.

See also x(), y(), setPos(), matrix(), and The Graphics View Coordinate System.

### void QGraphicsItem::prepareGeometryChange () `[protected]`

Prepares the item for a geometry change. Call this function before changing the bounding rect of an item to keep QGraphicsScene's index up to date.

prepareGeometryChange() will call update() if this is necessary.

Example:

     void CircleItem::setRadius(qreal newRadius)
     {
         if (radius != newRadius) {
             prepareGeometryChange();
             radius = newRadius;
         }
     }

See also boundingRect().

### void QGraphicsItem::removeSceneEventFilter ( QGraphicsItem * filterItem )

Removes an event filter on this item from filterItem.

See also installSceneEventFilter().

### void QGraphicsItem::resetMatrix ()

Resets this item's tranformation matrix to the identity matrix. This is equivalent to calling `setMatrix(QMatrix())`.

See also setMatrix() and matrix().

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

Rotates the current item transformation angle degrees clockwise.

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

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

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

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

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

Returns the current scene for the item, or 0 if the item is not stored in a scene.

Too add or move an item to a scene, call QGraphicsScene::addItem().

### QRectF QGraphicsItem::sceneBoundingRect () const

Returns the bounding rect of this item in scene coordinates, by combining sceneMatrix() with boundingRect().

See also boundingRect() and The Graphics View Coordinate System.

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

This virtual function receives events to this item. Reimplement this function to intercept events before they are dispatched to the specialized event handlers contextMenuEvent(), focusInEvent(), focusOutEvent(), hoverEnterEvent(), hoverMoveEvent(), hoverLeaveEvent(), keyPressEvent(), keyReleaseEvent(), mousePressEvent(), mouseReleaseEvent(), mouseMoveEvent(), and mouseDoubleClickEvent().

Returns true if the event was recognized and handled; otherwise, (e.g., if the event type was not recognized,) false is returned.

event is the intercepted event.

### bool QGraphicsItem::sceneEventFilter ( QGraphicsItem * watched, QEvent * event ) `[virtual protected]`

Filters events for the item watched. event is the filtered event. Reimplement this function after installing this item as an event filter for another item to intersect all the other item's events.

See also installSceneEventFilter().

### QMatrix QGraphicsItem::sceneMatrix () const

Returns this item's scene transformation matrix. This matrix can be used to map coordinates and geometrical shapes from this item's local coordinate system to the scene's coordinate system. To map coordinates from the scene, you must first invert the returned matrix.

Example:

     QGraphicsRectItem rect;
     rect.setPos(100, 100);

     rect.sceneMatrix().map(QPointF(0, 0));
     // returns QPointF(100, 100);

     rect.sceneMatrix().inverted().map(QPointF(100, 100));
     // returns QPointF(0, 0);

Unlike matrix(), which returns only an item's local transformation, this function includes the item's (and any parents') position.

See also matrix(), setMatrix(), scenePos(), and The Graphics View Coordinate System.

### QPointF QGraphicsItem::scenePos () const

Returns the item's position in scene coordinates. This is equivalent to calling `mapToScene(0, 0)`.

See also pos(), sceneMatrix(), and The Graphics View Coordinate System.

### void QGraphicsItem::setAcceptDrops ( bool on )

If on is true, this item will accept drag and drop events; otherwise, it is transparent for drag and drop events. By default, items do not accept drag and drop events.

See also acceptDrops().

### void QGraphicsItem::setAcceptedMouseButtons ( Qt::MouseButtons buttons )

Sets the mouse buttons that this item accepts mouse events for.

By default, all mouse buttons are accepted. If an item accepts a mouse button, it will become the mouse grabber item when a mouse press event is delivered for that button. However, if the item does not accept the mouse button, QGraphicsScene will forward the mouse events to the first item beneath it that does.

To disable mouse events for an item (i.e., make it transparent for mouse events), call setAcceptedMouseButtons(0).

See also acceptedMouseButtons() and mousePressEvent().

### void QGraphicsItem::setAcceptsHoverEvents ( bool enabled )

If enabled is true, this item will accept hover events; otherwise, it will ignore them. By default, items do not accept hover events.

Hover events are delivered when there is no current mouse grabber item. They are sent when the mouse cursor enters an item, when it moves around inside the item, and when the cursor leaves an item. Hover events are commonly used to highlight an item when it's entered, and for tracking the mouse cursor as it hovers over the item (equivalent to QWidget::mouseTracking).

Parent items receive hover enter events before their children, and leave events after their children. The parent does not receive a hover leave event if the cursor enters a child, though; the parent stays "hovered" until the cursor leaves its area, including its children's areas.

If a parent item handles child events (setHandlesChildEvents()), it will receive hover move, drag move, and drop events as the cursor passes through its children, but it does not receive hover enter and hover leave, nor drag enter and drag leave events on behalf of its children.

See also acceptsHoverEvents(), hoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent().

### void QGraphicsItem::setCursor ( const QCursor & cursor )

Sets the current cursor shape for the item to cursor. The mouse cursor will assume this shape when it's over this item. See the list of predefined cursor objects for a range of useful shapes.

An editor item might want to use an I-beam cursor:

     item->setCursor(Qt::IBeamCursor);

If no cursor has been set, the parent's cursor is used.

See also cursor(), hasCursor(), unsetCursor(), QWidget::cursor, and QApplication::overrideCursor().

### void QGraphicsItem::setData ( int key, const QVariant & value )

Sets this item's custom data for the key key to value.

Custom item data is useful for storing arbitrary properties for any item. Qt does not use this feature for storing data; it is provided solely for the convenience of the user.

See also data().

### void QGraphicsItem::setEnabled ( bool enabled )

If enabled is true, the item is enabled; otherwise, it is disabled.

Disabled items are visible, but they do not receive any events, and cannot take focus nor be selected. Mouse events are discarded; they are not propagated unless the item is also invisible, or if it does not accept mouse events (see acceptedMouseButtons()). A disabled item cannot become the mouse grabber, and as a result of this, an item loses the grab if it becomes disabled when grabbing the mouse, just like it loses focus if it had focus when it was disabled.

Disabled items are traditionally drawn using grayed-out colors (see QPalette::Disabled).

Items are enabled by default.

See also isEnabled().

### void QGraphicsItem::setFlag ( GraphicsItemFlag flag, bool enabled = true )

If enabled is true, the item flag flag is enabled; otherwise, it is disabled.

See also flags() and setFlags().

### void QGraphicsItem::setFlags ( GraphicsItemFlags flags )

Sets the item flags to flags. All flags in flags are enabled; all flags not in flags are disabled.

If the item had focus and flags does not enable ItemIsFocusable, the item loses focus as a result of calling this function. Similarily, if the item was selected, and flags does not enabled ItemIsSelectable, the item is automatically unselected.

By default, no flags are enabled.

See also flags() and setFlag().

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

Gives keyboard input focus to this item. The focusReason argument will be passed into any focus event generated by this function; it is used to give an explanation of what caused the item to get focus.

Only items that set the ItemIsFocusable flag can accept keyboard focus.

If this item is not visible (i.e., isVisible() returns false), not enabled, not associated with a scene, or if it already has input focus, this function will do nothing.

As a result of calling this function, this item will receive a focus in event with focusReason. If another item already has focus, that item will first receive a focus out event indicating that it has lost input focus.

See also clearFocus() and hasFocus().

### void QGraphicsItem::setGroup ( QGraphicsItemGroup * group )

Adds this item to the item group group. If group is 0, this item is removed from any current group and added as a child of the previous group's parent.

See also group() and QGraphicsScene::createItemGroup().

### void QGraphicsItem::setHandlesChildEvents ( bool enabled )

If enabled is true, this item is set to handle all events for all its children (i.e., all events intented for any of its children are instead sent to this item); otherwise, if enabled is false, this item will only handle its own events. The default value is false.

This property is useful for item groups; it allows one item to handle events on behalf of its children, as opposed to its children handling their events individually.

If a child item accepts hover events, its parent will receive hover move events as the cursor passes through the child, but it does not receive hover enter and hover leave events on behalf of its child.

See also handlesChildEvents().

### void QGraphicsItem::setMatrix ( const QMatrix & matrix, bool combine = false )

Sets the item'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.

To simplify interation with items using a transformed view, QGraphicsItem provides mapTo... and mapFrom... functions that can translate between items' and the scene's coordinates. For example, you can call mapToScene() to map an item coordiate to a scene coordinate, or mapFromScene() to map from scene coordinates to item coordinates.

See also matrix(), rotate(), scale(), shear(), translate(), and The Graphics View Coordinate System.

### void QGraphicsItem::setParentItem ( QGraphicsItem * parent )

Sets this item's parent item to parent. If this item already has a parent, it is first removed from the previous parent. If parent is 0, this item will become a top-level item.

See also parentItem() and children().

### void QGraphicsItem::setPos ( const QPointF & pos )

Sets the position of the item to pos, which is in parent coordinates. For items with no parent, pos is in scene coordinates.

The position of the item describes its origin (local coordinate (0, 0)) in parent coordinates.

See also pos(), scenePos(), and The Graphics View Coordinate System.

### void QGraphicsItem::setPos ( qreal x, qreal y )

This is an overloaded member function, provided for convenience.

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

### void QGraphicsItem::setSelected ( bool selected )

If selected is true and this item is selectable, this item is selected; otherwise, it is unselected.

If the item is in a group, the whole group's selected state is toggled by this function. If the group is selected, all items in the group are also selected, and if the group is not selected, no item in the group is selected.

Only visible, enabled, selectable items can be selected. If selected is true and this item is either invisible or disabled or unselectable, this function does nothing.

By default, items cannot be selected. To enable selection, set the ItemIsSelectable flag.

This function is provided for convenience, allowing individual toggling of the selected state of an item. However, a more common way of selecting items is to call QGraphicsScene::setSelectionArea(), which will call this function for all visible, enabled, and selectable items within a specified area on the scene.

See also isSelected() and QGraphicsScene::selectedItems().

### void QGraphicsItem::setToolTip ( const QString & toolTip )

Sets the item's tool tip to toolTip. If toolTip is empty, the item's tool tip is cleared.

See also toolTip() and QToolTip.

### void QGraphicsItem::setVisible ( bool visible )

If visible is true, the item is made visible. Otherwise, the item is made invisible. Invisible items are not painted, nor do they receive any events. In particular, mouse events pass right through invisible items, and are delivered to any item that may be behind. Invisible items are also unselectable, they cannot take input focus, and are not detected by QGraphicsScene's item location functions.

If an item becomes invisible while grabbing the mouse, (i.e., while it is receiving mouse events,) it will automatically lose the mouse grab, and the grab is not regained by making the item visible again; it must receive a new mouse press to regain the mouse grab.

Similarily, an invisible item cannot have focus, so if the item has focus when it becomes invisible, it will lose focus, and the focus is not regained by simply making the item visible again.

Items are visible by default; it is unnecessary to call setVisible() on a new item.

See also isVisible(), show(), and hide().

### void QGraphicsItem::setZValue ( qreal z )

Sets the Z-value, or the elevation, of the item, to z. The elevation decides the stacking order of sibling (neighboring) items. An item of high Z-value will be drawn on top of an item with a lower Z-value if they share the same parent item. In addition, children of an item will always be drawn on top of the parent, regardless of the child's Z-value. Sibling items that share the same Z-value will be drawn in an undefined order, although the order will stay the same for as long as the items live.

Children of different parents are stacked according to the Z-value of each item's ancestor item which is an immediate child of the two items' closest common ancestor. For example, a robot item might define a torso item as the parent of a head item, two arm items, and two upper-leg items. The upper-leg items would each be parents of one lower-leg item, and each lower-leg item would be parents of one foot item. The stacking order of the feet is the same as the stacking order of each foot's ancestor that is an immediate child of the two feet's common ancestor (i.e., the torso item); so the feet are stacked in the same order as the upper-leg items, regardless of each foot's Z-value.

The Z-value does not affect the item's size in any way.

The default Z-value is 0.

See also zValue().

### QPainterPath QGraphicsItem::shape () const `[virtual]`

Returns the shape of this item as a QPainterPath in local coordinates. The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.

The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection. For example:

     QPainterPath RoundItem::shape() const
     {
         QPainterPath path;
         path.addEllipse(boundingRect());
         return path;
     }

This function is called by the default implementations of contains() and collidesWithPath().

See also boundingRect() and contains().

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

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

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

### void QGraphicsItem::show ()

Shows the item. (Items are visible by default.)

This convenience function is equivalent to calling `setVisible(true)`.

See also hide() and setVisible().

### QString QGraphicsItem::toolTip () const

Returns the item's tool tip, or an empty QString if no tool tip has been set.

See also setToolTip() and QToolTip.

### QGraphicsItem * QGraphicsItem::topLevelItem () const

Returns this item's top-level item. The top-level item is the item's topmost ancestor item whose parent is 0. If an item has no parent, its own pointer is returned (i.e., a top-level item is its own top-level item).

See also parentItem().

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

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

If all you want is to move an item, you should call moveBy() or setPos() instead; this function changes the item's translation, which is conceptually separate from its position.

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

### int QGraphicsItem::type () const `[virtual]`

Returns the type of an item as an int. All standard graphicsitem classes are associated with a unique value; see QGraphicsItem::Type. This type information is used by qgraphicsitem_cast() to distinguish between types.

Reimplementing this function and declaring a Type enum value equal to your custom item's type will enable use of qgraphicsitem_cast() with the item. Custom items must return a value larger than or equal to UserType (65536).

The default implementation (in QGraphicsItem) returns UserType.

### void QGraphicsItem::unsetCursor ()

Clears the cursor from this item.

See also hasCursor() and setCursor().

### void QGraphicsItem::update ( const QRectF & rect = QRectF() )

Schedules a redraw of the area covered by rect in this item. You can call this function whenever your item needs to be redrawn, such as if it changes appearance or size.

This function does not cause an immediate paint; instead it schedules a paint request that is processed by QGraphicsView after control reaches the event loop. The item will only be redrawn if it is visible in any associated view.

As a side effect of the item being repainted, other items that overlap the area rect may also be repainted.

If the item is invisible (i.e., isVisible() returns false), this function does nothing.

See also paint() and boundingRect().

### void QGraphicsItem::update ( qreal x, qreal y, qreal width, qreal height )

This is an overloaded member function, provided for convenience.

This convenience function is equivalent to calling update(QRectF(x, y, width, height)).

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

This event handler, for event event, can be reimplemented to receive wheel events for this item. If you reimplement this function, event will be accepted by default.

If you ignore the event, (i.e., by calling QEvent::ignore(),) it will propagate to any item beneath this item. If no items accept the event, it will be ignored by the scene, and propagate to the view (e.g., the view's vertical scroll bar).

The default implementation ignores the event.

See also sceneEvent().

### qreal QGraphicsItem::x () const

This convenience function is equivalent to calling pos().x().

See also y().

### qreal QGraphicsItem::y () const

This convenience function is equivalent to calling pos().y().

See also x().

### qreal QGraphicsItem::zValue () const

Returns the Z-value, or the elevation, of the item. The Z-value decides the stacking order of sibling (neighboring) items.

The default Z-value is 0.

See also setZValue().

## Member Variable Documentation

### const int QGraphicsItem::UserType

The lowest permitted type value for custom items (subclasses of QGraphicsItem or any of the standard items). This value is used in conjunction with a reimplementation of QGraphicsItem::type() and declaring a Type enum value. Example:

     class CustomItem : public QGraphicsItem
     {
        ...
        enum { Type = UserType + 1 }

        int type() const
        {
            // Enable the use of qgraphicsitem_cast with this item.
            return Type;
        }
        ...
     };

## Related Non-Members

### T qgraphicsitem_cast ( QGraphicsItem * item )

Returns the given item cast to type T if item is of type T; otherwise, 0 is returned.

This function was introduced in Qt 4.2.