Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QGraphicsScene Class Reference

Inheritance diagram for QGraphicsScene:

QObject DiagramScene DiagramScene

List of all members.

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.


doc/src/snippets/code/src_gui_graphicsview_qgraphicsscene.cpp 0

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. The dimensions of the items added with these functions are relative to the item's coordinate system, and the items position is initialized to (0, 0) in the scene.

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

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

Definition at line 102 of file qgraphicsscene.h.

Public Types

enum  ItemIndexMethod { BspTreeIndex, NoIndex = -1 }
enum  SceneLayer { ItemLayer = 0x1, BackgroundLayer = 0x2, ForegroundLayer = 0x4, AllLayers = 0xffff }

Public Slots

void advance ()
void clear ()
void clearSelection ()
void deleteLater ()
void invalidate (const QRectF &rect=QRectF(), SceneLayers layers=AllLayers)
void update (const QRectF &rect=QRectF())


void changed (const QList< QRectF > &region)
void destroyed (QObject *=0)
void sceneRectChanged (const QRectF &rect)
void selectionChanged ()

Public Member Functions

QGraphicsWidgetactiveWindow () const
QGraphicsEllipseItemaddEllipse (qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsEllipseItemaddEllipse (const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
void addItem (QGraphicsItem *item)
QGraphicsLineItemaddLine (qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen=QPen())
QGraphicsLineItemaddLine (const QLineF &line, const QPen &pen=QPen())
QGraphicsPathItemaddPath (const QPainterPath &path, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsPixmapItemaddPixmap (const QPixmap &pixmap)
QGraphicsPolygonItemaddPolygon (const QPolygonF &polygon, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsRectItemaddRect (qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsRectItemaddRect (const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsSimpleTextItemaddSimpleText (const QString &text, const QFont &font=QFont())
QGraphicsTextItemaddText (const QString &text, const QFont &font=QFont())
QGraphicsProxyWidgetaddWidget (QWidget *widget, Qt::WindowFlags wFlags=0)
QBrush backgroundBrush () const
bool blockSignals (bool b)
int bspTreeDepth () const
const QObjectListchildren () const
void clearFocus ()
QList< QGraphicsItem * > collidingItems (const QGraphicsItem *item, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
QGraphicsItemGroupcreateItemGroup (const QList< QGraphicsItem * > &items)
void destroyItemGroup (QGraphicsItemGroup *group)
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
QList< QByteArraydynamicPropertyNames () const
template<typename T>
findChild (const QString &aName=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &aName=QString()) const
QGraphicsItemfocusItem () const
QFont font () const
QBrush foregroundBrush () const
bool hasFocus () const
qreal height () const
bool inherits (const char *classname) const
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const
void installEventFilter (QObject *)
void invalidate (qreal x, qreal y, qreal w, qreal h, SceneLayers layers=AllLayers)
bool isWidgetType () const
QGraphicsItemitemAt (qreal x, qreal y) const
QGraphicsItemitemAt (const QPointF &pos) const
ItemIndexMethod itemIndexMethod () const
QList< QGraphicsItem * > items (qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
QList< QGraphicsItem * > items (const QPainterPath &path, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
QList< QGraphicsItem * > items (const QPolygonF &polygon, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
QList< QGraphicsItem * > items (const QRectF &rect, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
QList< QGraphicsItem * > items (const QPointF &pos) const
QList< QGraphicsItem * > items () const
QRectF itemsBoundingRect () const
void killTimer (int id)
QGraphicsItemmouseGrabberItem () const
void moveToThread (QThread *thread)
QString objectName () const
QPalette palette () const
QObjectparent () const
QVariant property (const char *name) const
 QGraphicsScene (qreal x, qreal y, qreal width, qreal height, QObject *parent=0)
 QGraphicsScene (const QRectF &sceneRect, QObject *parent=0)
 QGraphicsScene (QObject *parent=0)
void removeEventFilter (QObject *)
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
QPainterPath selectionArea () const
void setActiveWindow (QGraphicsWidget *widget)
void setBackgroundBrush (const QBrush &brush)
void setBspTreeDepth (int depth)
void setFocus (Qt::FocusReason focusReason=Qt::OtherFocusReason)
void setFocusItem (QGraphicsItem *item, Qt::FocusReason focusReason=Qt::OtherFocusReason)
void setFont (const QFont &font)
void setForegroundBrush (const QBrush &brush)
void setItemIndexMethod (ItemIndexMethod method)
void setObjectName (const QString &name)
void setPalette (const QPalette &palette)
void setParent (QObject *)
bool setProperty (const char *name, const QVariant &value)
void setSceneRect (qreal x, qreal y, qreal w, qreal h)
void setSceneRect (const QRectF &rect)
void setSelectionArea (const QPainterPath &path, Qt::ItemSelectionMode)
void setSelectionArea (const QPainterPath &path)
void setStyle (QStyle *style)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
int startTimer (int interval)
QStylestyle () const
QThreadthread () const
void update (qreal x, qreal y, qreal w, qreal h)
QObjectUserData * userData (uint id) const
QList< QGraphicsView * > views () const
qreal width () const
virtual ~QGraphicsScene ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Slots

bool focusNextPrevChild (bool next)

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void contextMenuEvent (QGraphicsSceneContextMenuEvent *event)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
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)
bool event (QEvent *event)
bool eventFilter (QObject *watched, QEvent *event)
virtual void focusInEvent (QFocusEvent *event)
virtual void focusOutEvent (QFocusEvent *event)
virtual void helpEvent (QGraphicsSceneHelpEvent *event)
virtual void inputMethodEvent (QInputMethodEvent *event)
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)
int receivers (const char *signal) const
QObjectsender () const
virtual void timerEvent (QTimerEvent *)
virtual void wheelEvent (QGraphicsSceneWheelEvent *event)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject


QBrush backgroundBrush
 the background brush of the scene.
int bspTreeDepth
 the depth of QGraphicsScene's BSP index tree
QFont font
 the scene's default font
QBrush foregroundBrush
 the foreground brush of the scene.
ItemIndexMethod itemIndexMethod
 the item indexing method.
QString objectName
 the name of this object
QPalette palette
 the scene's default palette
QRectF sceneRect
 the scene rectangle; the bounding rectangle of the scene

Private Member Functions

void _q_emitUpdated ()) Q_PRIVATE_SLOT(d_func()
void void void void _q_polishItems ()) friend class QGraphicsItem
void void _q_removeItemLater (QGraphicsItem *item)) Q_PRIVATE_SLOT(d_func()
void void void _q_updateLater ()) Q_PRIVATE_SLOT(d_func()
void itemUpdated (QGraphicsItem *item, const QRectF &rect)
 Q_PRIVATE_SLOT (d_func(), void _q_updateIndex()) Q_PRIVATE_SLOT(d_func()


class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
class QGraphicsItemPrivate
class QGraphicsView
class QGraphicsViewPrivate
class QGraphicsWidget
struct QMetaObject
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

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

Generated by  Doxygen 1.6.0   Back to index