Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QGraphicsScene Class Reference

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

#include <qgraphicsscene.h>

+ Inheritance diagram for QGraphicsScene:
+ Collaboration diagram for QGraphicsScene:

Public Types

enum  ItemIndexMethod { BspTreeIndex , NoIndex = -1 }
 This enum describes the indexing algorithms QGraphicsScene provides for managing positional information about items on the scene. More...
 
enum  SceneLayer { ItemLayer = 0x1 , BackgroundLayer = 0x2 , ForegroundLayer = 0x4 , AllLayers = 0xffff }
 

Public Slots

void update (const QRectF &rect=QRectF())
 Schedules a redraw of the area rect on the scene.
 
void invalidate (const QRectF &rect=QRectF(), SceneLayers layers=AllLayers)
 Invalidates and schedules a redraw of the layers in rect on the scene.
 
void advance ()
 This slot advances the scene by one step, by calling QGraphicsItem::advance() for all items on the scene.
 
void clearSelection ()
 Clears the current selection.
 
void clear ()
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void changed (const QList< QRectF > &region)
 This signal is emitted by QGraphicsScene when control reaches the event loop, if the scene content changes.
 
void sceneRectChanged (const QRectF &rect)
 This signal is emitted by QGraphicsScene whenever the scene rect changes.
 
void selectionChanged ()
 
void focusItemChanged (QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason)
 This signal is emitted by QGraphicsScene whenever focus changes in the scene (i.e., when an item gains or loses input focus, or when focus passes from one item to another).
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QGraphicsScene (QObject *parent=nullptr)
 Constructs a QGraphicsScene object.
 
 QGraphicsScene (const QRectF &sceneRect, QObject *parent=nullptr)
 Constructs a QGraphicsScene object, using sceneRect for its scene rectangle.
 
 QGraphicsScene (qreal x, qreal y, qreal width, qreal height, QObject *parent=nullptr)
 Constructs a QGraphicsScene object, using the rectangle specified by (x, y), and the given width and height for its scene rectangle.
 
virtual ~QGraphicsScene ()
 Removes and deletes all items from the scene object before destroying the scene object.
 
QRectF sceneRect () const
 
qreal width () const
 This convenience function is equivalent to calling sceneRect().width().
 
qreal height () const
 This convenience function is equivalent to calling sceneRect().height().
 
void setSceneRect (const QRectF &rect)
 
void setSceneRect (qreal x, qreal y, qreal w, qreal h)
 
void render (QPainter *painter, const QRectF &target=QRectF(), const QRectF &source=QRectF(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
 Renders the source rect from scene into target, using painter.
 
ItemIndexMethod itemIndexMethod () const
 
void setItemIndexMethod (ItemIndexMethod method)
 
int bspTreeDepth () const
 
void setBspTreeDepth (int depth)
 
QRectF itemsBoundingRect () const
 Calculates and returns the bounding rect of all items on the scene.
 
QList< QGraphicsItem * > items (Qt::SortOrder order=Qt::DescendingOrder) const
 Returns an ordered list of all items on the scene.
 
QList< QGraphicsItem * > items (const QPointF &pos, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape, Qt::SortOrder order=Qt::DescendingOrder, const QTransform &deviceTransform=QTransform()) const
 Returns all visible items that, depending on mode, are at the specified pos in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.
 
QList< QGraphicsItem * > items (const QRectF &rect, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape, Qt::SortOrder order=Qt::DescendingOrder, const QTransform &deviceTransform=QTransform()) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QList< QGraphicsItem * > items (const QPolygonF &polygon, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape, Qt::SortOrder order=Qt::DescendingOrder, const QTransform &deviceTransform=QTransform()) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QList< QGraphicsItem * > items (const QPainterPath &path, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape, Qt::SortOrder order=Qt::DescendingOrder, const QTransform &deviceTransform=QTransform()) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QList< QGraphicsItem * > items (qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order, const QTransform &deviceTransform=QTransform()) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QList< QGraphicsItem * > collidingItems (const QGraphicsItem *item, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape) const
 Returns a list of all items that collide with item.
 
QGraphicsItemitemAt (const QPointF &pos, const QTransform &deviceTransform) const
 
QGraphicsItemitemAt (qreal x, qreal y, const QTransform &deviceTransform) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QList< QGraphicsItem * > selectedItems () const
 Returns a list of all currently selected items.
 
QPainterPath selectionArea () const
 Returns the selection area that was previously set with setSelectionArea(), or an empty QPainterPath if no selection area has been set.
 
void setSelectionArea (const QPainterPath &path, const QTransform &deviceTransform)
 
void setSelectionArea (const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation=Qt::ReplaceSelection, Qt::ItemSelectionMode mode=Qt::IntersectsItemShape, const QTransform &deviceTransform=QTransform())
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QGraphicsItemGroupcreateItemGroup (const QList< QGraphicsItem * > &items)
 Groups all items in items into a new QGraphicsItemGroup, and returns a pointer to the group.
 
void destroyItemGroup (QGraphicsItemGroup *group)
 Reparents all items in group to group's parent item, then removes group from the scene, and finally deletes it.
 
void addItem (QGraphicsItem *item)
 Adds or moves the item and all its children to this scene.
 
QGraphicsEllipseItemaddEllipse (const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 Creates and adds an ellipse item to the scene, and returns the item pointer.
 
QGraphicsLineItemaddLine (const QLineF &line, const QPen &pen=QPen())
 Creates and adds a line item to the scene, and returns the item pointer.
 
QGraphicsPathItemaddPath (const QPainterPath &path, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 Creates and adds a path item to the scene, and returns the item pointer.
 
QGraphicsPixmapItemaddPixmap (const QPixmap &pixmap)
 Creates and adds a pixmap item to the scene, and returns the item pointer.
 
QGraphicsPolygonItemaddPolygon (const QPolygonF &polygon, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 Creates and adds a polygon item to the scene, and returns the item pointer.
 
QGraphicsRectItemaddRect (const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 Creates and adds a rectangle item to the scene, and returns the item pointer.
 
QGraphicsTextItemaddText (const QString &text, const QFont &font=QFont())
 Creates and adds a text item to the scene, and returns the item pointer.
 
QGraphicsSimpleTextItemaddSimpleText (const QString &text, const QFont &font=QFont())
 Creates and adds a QGraphicsSimpleTextItem to the scene, and returns the item pointer.
 
QGraphicsProxyWidgetaddWidget (QWidget *widget, Qt::WindowFlags wFlags=Qt::WindowFlags())
 Creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy.
 
QGraphicsEllipseItemaddEllipse (qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 
QGraphicsLineItemaddLine (qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen=QPen())
 
QGraphicsRectItemaddRect (qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
 
void removeItem (QGraphicsItem *item)
 Removes the item item and all its children from the scene.
 
QGraphicsItemfocusItem () const
 When the scene is active, this functions returns the scene's current focus item, or \nullptr if no item currently has focus.
 
void setFocusItem (QGraphicsItem *item, Qt::FocusReason focusReason=Qt::OtherFocusReason)
 Sets the scene's focus item to item, with the focus reason focusReason, after removing focus from any previous item that may have had focus.
 
bool hasFocus () const
 Returns true if the scene has focus; otherwise returns false.
 
void setFocus (Qt::FocusReason focusReason=Qt::OtherFocusReason)
 Sets focus on the scene by sending a QFocusEvent to the scene, passing focusReason as the reason.
 
void clearFocus ()
 Clears focus from the scene.
 
void setStickyFocus (bool enabled)
 
bool stickyFocus () const
 
QGraphicsItemmouseGrabberItem () const
 Returns the current mouse grabber item, or \nullptr if no item is currently grabbing the mouse.
 
QBrush backgroundBrush () const
 
void setBackgroundBrush (const QBrush &brush)
 
QBrush foregroundBrush () const
 
void setForegroundBrush (const QBrush &brush)
 
virtual QVariant inputMethodQuery (Qt::InputMethodQuery query) const
 This method is used by input methods to query a set of properties of the scene to be able to support complex input method operations as support for surrounding text and reconversions.
 
QList< QGraphicsView * > views () const
 Returns a list of all the views that display this scene.
 
void update (qreal x, qreal y, qreal w, qreal h)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
void invalidate (qreal x, qreal y, qreal w, qreal h, SceneLayers layers=AllLayers)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QStylestyle () const
 
void setStyle (QStyle *style)
 
QFont font () const
 
void setFont (const QFont &font)
 
QPalette palette () const
 
void setPalette (const QPalette &palette)
 
bool isActive () const
 
QGraphicsItemactivePanel () const
 
void setActivePanel (QGraphicsItem *item)
 
QGraphicsWidgetactiveWindow () const
 
void setActiveWindow (QGraphicsWidget *widget)
 
bool sendEvent (QGraphicsItem *item, QEvent *event)
 
qreal minimumRenderSize () const
 
void setMinimumRenderSize (qreal minSize)
 
bool focusOnTouch () const
 
void setFocusOnTouch (bool enabled)
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Protected Slots

virtual bool focusNextPrevChild (bool next)
 

Protected Member Functions

bool event (QEvent *event) override
 Processes the event event, and dispatches it to the respective event handlers.
 
bool eventFilter (QObject *watched, QEvent *event) override
 \reimp
 
virtual void contextMenuEvent (QGraphicsSceneContextMenuEvent *event)
 This event handler, for event contextMenuEvent, can be reimplemented in a subclass to receive context menu events.
 
virtual void dragEnterEvent (QGraphicsSceneDragDropEvent *event)
 This event handler, for event event, can be reimplemented in a subclass to receive drag enter events for the scene.
 
virtual void dragMoveEvent (QGraphicsSceneDragDropEvent *event)
 This event handler, for event event, can be reimplemented in a subclass to receive drag move events for the scene.
 
virtual void dragLeaveEvent (QGraphicsSceneDragDropEvent *event)
 This event handler, for event event, can be reimplemented in a subclass to receive drag leave events for the scene.
 
virtual void dropEvent (QGraphicsSceneDragDropEvent *event)
 This event handler, for event event, can be reimplemented in a subclass to receive drop events for the scene.
 
virtual void focusInEvent (QFocusEvent *event)
 This event handler, for event focusEvent, can be reimplemented in a subclass to receive focus in events.
 
virtual void focusOutEvent (QFocusEvent *event)
 This event handler, for event focusEvent, can be reimplemented in a subclass to receive focus out events.
 
virtual void helpEvent (QGraphicsSceneHelpEvent *event)
 This event handler, for event helpEvent, can be reimplemented in a subclass to receive help events.
 
virtual void keyPressEvent (QKeyEvent *event)
 This event handler, for event keyEvent, can be reimplemented in a subclass to receive keypress events.
 
virtual void keyReleaseEvent (QKeyEvent *event)
 This event handler, for event keyEvent, can be reimplemented in a subclass to receive key release events.
 
virtual void mousePressEvent (QGraphicsSceneMouseEvent *event)
 This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse press events for the scene.
 
virtual void mouseMoveEvent (QGraphicsSceneMouseEvent *event)
 This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse move events for the scene.
 
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *event)
 This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse release events for the scene.
 
virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event)
 This event handler, for event mouseEvent, can be reimplemented in a subclass to receive mouse double-click events for the scene.
 
virtual void wheelEvent (QGraphicsSceneWheelEvent *event)
 This event handler, for event wheelEvent, can be reimplemented in a subclass to receive mouse wheel events for the scene.
 
virtual void inputMethodEvent (QInputMethodEvent *event)
 This event handler, for event event, can be reimplemented in a subclass to receive input method events for the scene.
 
virtual void drawBackground (QPainter *painter, const QRectF &rect)
 Draws the background of the scene using painter, before any items and the foreground are drawn.
 
virtual void drawForeground (QPainter *painter, const QRectF &rect)
 Draws the foreground of the scene using painter, after the background and all items have been drawn.
 
virtual void drawItems (QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[], QWidget *widget=nullptr)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 

Properties

QBrush backgroundBrush
 the background brush of the scene.
 
QBrush foregroundBrush
 the foreground brush of the scene.
 
ItemIndexMethod itemIndexMethod
 the item indexing method.
 
QRectF sceneRect
 the scene rectangle; the bounding rectangle of the scene
 
int bspTreeDepth
 the depth of QGraphicsScene's BSP index tree
 
QPalette palette
 the scene's default palette
 
QFont font
 the scene's default font
 
bool stickyFocus
 whether clicking into the scene background will clear focus
 
qreal minimumRenderSize
 the minimal view-transformed size an item must have to be drawn
 
bool focusOnTouch
 whether items gain focus when receiving a {touch begin} event.
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Friends

class QGraphicsItemPrivate
 
class QGraphicsObject
 
class QGraphicsView
 
class QGraphicsViewPrivate
 
class QGraphicsWidget
 
class QGraphicsWidgetPrivate
 
class QGraphicsEffect
 
class QGraphicsSceneIndex
 
class QGraphicsSceneIndexPrivate
 
class QGraphicsSceneBspTreeIndex
 
class QGraphicsSceneBspTreeIndexPrivate
 
class QGraphicsItemEffectSourcePrivate
 
class QGesture
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

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

Since
4.2

\inmodule QtWidgets

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 \l{Graphics View Framework}.

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

Example:

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 a 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 58 of file qgraphicsscene.h.

Member Enumeration Documentation

◆ ItemIndexMethod

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

\value BspTreeIndex A Binary Space Partitioning tree is applied. All QGraphicsScene's item location algorithms are of an order close to logarithmic complexity, by making use of binary search. Adding, moving and removing items is logarithmic. This approach is best for static scenes (i.e., scenes where most items do not move).

\value NoIndex No index is applied. Item location is of linear complexity, as all items on the scene are searched. Adding, moving and removing items, however, is done in constant time. This approach is ideal for dynamic scenes, where many items are added, moved or removed continuously.

See also
setItemIndexMethod(), bspTreeDepth
Enumerator
BspTreeIndex 
NoIndex 

Definition at line 73 of file qgraphicsscene.h.

◆ SceneLayer

Since
4.3

This enum describes the rendering layers in a QGraphicsScene. When QGraphicsScene draws the scene contents, it renders each of these layers separately, in order.

Each layer represents a flag that can be OR'ed together when calling functions such as invalidate() or QGraphicsView::invalidateScene().

\value ItemLayer The item layer. QGraphicsScene renders all items are in this layer by calling the virtual function drawItems(). The item layer is drawn after the background layer, but before the foreground layer.

\value BackgroundLayer The background layer. QGraphicsScene renders the scene's background in this layer by calling the virtual function drawBackground(). The background layer is drawn first of all layers.

\value ForegroundLayer The foreground layer. QGraphicsScene renders the scene's foreground in this layer by calling the virtual function drawForeground(). The foreground layer is drawn last of all layers.

\value AllLayers All layers; this value represents a combination of all three layers.

See also
invalidate(), QGraphicsView::invalidateScene()
Enumerator
ItemLayer 
BackgroundLayer 
ForegroundLayer 
AllLayers 

Definition at line 78 of file qgraphicsscene.h.

Constructor & Destructor Documentation

◆ QGraphicsScene() [1/3]

QGraphicsScene::QGraphicsScene ( QObject * parent = nullptr)

Constructs a QGraphicsScene object.

The parent parameter is passed to QObject's constructor.

Definition at line 1611 of file qgraphicsscene.cpp.

◆ QGraphicsScene() [2/3]

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

Constructs a QGraphicsScene object, using sceneRect for its scene rectangle.

The parent parameter is passed to QObject's constructor.

See also
sceneRect

Definition at line 1624 of file qgraphicsscene.cpp.

◆ QGraphicsScene() [3/3]

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

Constructs a QGraphicsScene object, using the rectangle specified by (x, y), and the given width and height for its scene rectangle.

The parent parameter is passed to QObject's constructor.

See also
sceneRect

Definition at line 1639 of file qgraphicsscene.cpp.

◆ ~QGraphicsScene()

QGraphicsScene::~QGraphicsScene ( )
virtual

Removes and deletes all items from the scene object before destroying the scene object.

The scene object is removed from the application's global scene list, and it is removed from all associated views.

Definition at line 1652 of file qgraphicsscene.cpp.

Member Function Documentation

◆ activePanel()

QGraphicsItem * QGraphicsScene::activePanel ( ) const
Since
4.6 Returns the current active panel, or \nullptr if no panel is currently active.
See also
QGraphicsScene::setActivePanel()

Definition at line 5608 of file qgraphicsscene.cpp.

◆ activeWindow()

QGraphicsWidget * QGraphicsScene::activeWindow ( ) const
Since
4.4

Returns the current active window, or \nullptr if no window is currently active.

See also
QGraphicsScene::setActiveWindow()

Definition at line 5639 of file qgraphicsscene.cpp.

◆ addEllipse() [1/2]

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

Creates and adds an ellipse item to the scene, and returns the item pointer.

The geometry of the ellipse is defined by rect, and its pen and brush are initialized to pen and brush.

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

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

See also
addLine(), addPath(), addPixmap(), addRect(), addText(), addItem(), addWidget()

Definition at line 2582 of file qgraphicsscene.cpp.

◆ addEllipse() [2/2]

QGraphicsEllipseItem * QGraphicsScene::addEllipse ( qreal x,
qreal y,
qreal w,
qreal h,
const QPen & pen = QPen(),
const QBrush & brush = QBrush() )
inline
Since
4.3

This convenience function is equivalent to calling addEllipse(QRectF(x, y, w, h), pen, brush).

Definition at line 143 of file qgraphicsscene.h.

◆ addItem()

void QGraphicsScene::addItem ( QGraphicsItem * item)

Adds or moves the item and all its children to this scene.

This scene takes ownership of the item.

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

If the item is already in a different scene, it will first be removed from its old scene, and then added to this scene as a top-level.

QGraphicsScene will send ItemSceneChange notifications to item while it is added to the scene. If item does not currently belong to a scene, only one notification is sent. If it does belong to scene already (i.e., it is moved to this scene), QGraphicsScene will send an addition notification as the item is removed from its previous scene.

If the item is a panel, the scene is active, and there is no active panel in the scene, then the item will be activated.

See also
removeItem(), addEllipse(), addLine(), addPath(), addPixmap(), addRect(), addText(), addWidget(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 2395 of file qgraphicsscene.cpp.

◆ addLine() [1/2]

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

Creates and adds a line item to the scene, and returns the item pointer.

The geometry of the line is defined by line, and its pen is initialized to pen.

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

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

See also
addEllipse(), addPath(), addPixmap(), addRect(), addText(), addItem(), addWidget()

Definition at line 2614 of file qgraphicsscene.cpp.

◆ addLine() [2/2]

QGraphicsLineItem * QGraphicsScene::addLine ( qreal x1,
qreal y1,
qreal x2,
qreal y2,
const QPen & pen = QPen() )
inline
Since
4.3

This convenience function is equivalent to calling addLine(QLineF(x1, y1, x2, y2), pen).

Definition at line 145 of file qgraphicsscene.h.

◆ addPath()

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

Creates and adds a path item to the scene, and returns the item pointer.

The geometry of the path is defined by path, and its pen and brush are initialized to pen and brush.

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

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

See also
addEllipse(), addLine(), addPixmap(), addRect(), addText(), addItem(), addWidget()

Definition at line 2645 of file qgraphicsscene.cpp.

◆ addPixmap()

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

Creates and adds a pixmap item to the scene, and returns the item pointer.

The pixmap is defined by pixmap.

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

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

See also
addEllipse(), addLine(), addPath(), addRect(), addText(), addItem(), addWidget()

Definition at line 2668 of file qgraphicsscene.cpp.

◆ addPolygon()

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

Creates and adds a polygon item to the scene, and returns the item pointer.

The polygon is defined by polygon, and its pen and brush are initialized to pen and brush.

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

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

See also
addEllipse(), addLine(), addPath(), addRect(), addText(), addItem(), addWidget()

Definition at line 2690 of file qgraphicsscene.cpp.

◆ addRect() [1/2]

QGraphicsRectItem * QGraphicsScene::addRect ( const QRectF & rect,
const QPen & pen = QPen(),
const QBrush & brush = QBrush() )

Creates and adds a rectangle item to the scene, and returns the item pointer.

The geometry of the rectangle is defined by rect, and its pen and brush are initialized to pen and brush.

Note that the item's geometry is provided in item coordinates, and its position is initialized to (0, 0). For example, if a QRect(50, 50, 100, 100) is added, its top-left corner will be at (50, 50) relative to the origin in the item's coordinate system.

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

See also
addEllipse(), addLine(), addPixmap(), addPixmap(), addText(), addItem(), addWidget()

Definition at line 2717 of file qgraphicsscene.cpp.

◆ addRect() [2/2]

QGraphicsRectItem * QGraphicsScene::addRect ( qreal x,
qreal y,
qreal w,
qreal h,
const QPen & pen = QPen(),
const QBrush & brush = QBrush() )
inline
Since
4.3

This convenience function is equivalent to calling addRect(QRectF(x, y, w, h), pen, brush).

Definition at line 147 of file qgraphicsscene.h.

◆ addSimpleText()

QGraphicsSimpleTextItem * QGraphicsScene::addSimpleText ( const QString & text,
const QFont & font = QFont() )

Creates and adds a QGraphicsSimpleTextItem to the scene, and returns the item pointer.

The text string is initialized to text, and its font is initialized to font.

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

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

See also
addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), addItem(), addWidget()

Definition at line 2770 of file qgraphicsscene.cpp.

◆ addText()

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

Creates and adds a text item to the scene, and returns the item pointer.

The text string is initialized to text, and its font is initialized to font.

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

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

See also
addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), addItem(), addWidget()

Definition at line 2748 of file qgraphicsscene.cpp.

◆ addWidget()

QGraphicsProxyWidget * QGraphicsScene::addWidget ( QWidget * widget,
Qt::WindowFlags wFlags = Qt::WindowFlags() )

Creates a new QGraphicsProxyWidget for widget, adds it to the scene, and returns a pointer to the proxy.

wFlags set the default window flags for the embedding proxy widget.

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

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

Note that widgets with the Qt::WA_PaintOnScreen widget attribute set and widgets that wrap an external application or controller are not supported. Examples are QOpenGLWidget and QAxWidget.

See also
addEllipse(), addLine(), addPixmap(), addPixmap(), addRect(), addText(), addSimpleText(), addItem()

Definition at line 2796 of file qgraphicsscene.cpp.

◆ advance

void QGraphicsScene::advance ( )
slot

This slot advances the scene by one step, by calling QGraphicsItem::advance() for all items on the scene.

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

Note that you can also use the \l{The Animation Framework}{Animation Framework} for animations.

See also
QGraphicsItem::advance(), QTimeLine

Definition at line 3202 of file qgraphicsscene.cpp.

◆ backgroundBrush()

QBrush QGraphicsScene::backgroundBrush ( ) const

Definition at line 3010 of file qgraphicsscene.cpp.

◆ bspTreeDepth()

int QGraphicsScene::bspTreeDepth ( ) const

Definition at line 1887 of file qgraphicsscene.cpp.

◆ changed

void QGraphicsScene::changed ( const QList< QRectF > & region)
signal

This signal is emitted by QGraphicsScene when control reaches the event loop, if the scene content changes.

The region parameter contains a list of scene rectangles that indicate the area that has been changed.

See also
QGraphicsView::updateScene()

◆ clear

void QGraphicsScene::clear ( )
slot
Since
4.4

Removes and deletes all items from the scene, but otherwise leaves the state of the scene unchanged.

See also
addItem()

Definition at line 2281 of file qgraphicsscene.cpp.

◆ clearFocus()

void QGraphicsScene::clearFocus ( )

Clears focus from the scene.

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

A scene that does not have focus ignores key events.

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

Definition at line 2927 of file qgraphicsscene.cpp.

◆ clearSelection

void QGraphicsScene::clearSelection ( )
slot

Clears the current selection.

See also
setSelectionArea(), selectedItems()

Definition at line 2247 of file qgraphicsscene.cpp.

◆ collidingItems()

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

Returns a list of all items that collide with item.

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

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

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

Definition at line 2067 of file qgraphicsscene.cpp.

◆ contextMenuEvent()

void QGraphicsScene::contextMenuEvent ( QGraphicsSceneContextMenuEvent * contextMenuEvent)
protectedvirtual

This event handler, for event contextMenuEvent, can be reimplemented in a subclass to receive context menu events.

The default implementation forwards the event to the topmost visible item that accepts context menu events at the position of the event. If no items accept context menu events at this position, the event is ignored.

Note: See items() for a definition of which items are considered visible by this function.

See also
QGraphicsItem::contextMenuEvent()

Definition at line 3478 of file qgraphicsscene.cpp.

◆ createItemGroup()

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

Groups all items in items into a new QGraphicsItemGroup, and returns a pointer to the group.

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

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

See also
destroyItemGroup(), QGraphicsItemGroup::addToGroup()

Definition at line 2311 of file qgraphicsscene.cpp.

◆ destroyItemGroup()

void QGraphicsScene::destroyItemGroup ( QGraphicsItemGroup * group)

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

The items' positions and transformations are mapped from the group to the group's parent.

See also
createItemGroup(), QGraphicsItemGroup::removeFromGroup()

Definition at line 2361 of file qgraphicsscene.cpp.

◆ dragEnterEvent()

void QGraphicsScene::dragEnterEvent ( QGraphicsSceneDragDropEvent * event)
protectedvirtual

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

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

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

Definition at line 3511 of file qgraphicsscene.cpp.

◆ dragLeaveEvent()

void QGraphicsScene::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event)
protectedvirtual

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

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

Definition at line 3612 of file qgraphicsscene.cpp.

◆ dragMoveEvent()

void QGraphicsScene::dragMoveEvent ( QGraphicsSceneDragDropEvent * event)
protectedvirtual

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

Note: See items() for a definition of which items are considered visible by this function.

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

Definition at line 3528 of file qgraphicsscene.cpp.

◆ drawBackground()

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

Draws the background of the scene using painter, before any items and the foreground are drawn.

Reimplement this function to provide a custom background for the scene.

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

If all you want is to define a color, texture, or gradient for the background, you can call setBackgroundBrush() instead.

See also
drawForeground(), drawItems()

Definition at line 4143 of file qgraphicsscene.cpp.

◆ drawForeground()

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

Draws the foreground of the scene using painter, after the background and all items have been drawn.

Reimplement this function to provide a custom foreground for the scene.

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

If all you want is to define a color, texture or gradient for the foreground, you can call setForegroundBrush() instead.

See also
drawBackground(), drawItems()

Definition at line 4170 of file qgraphicsscene.cpp.

◆ drawItems()

void QGraphicsScene::drawItems ( QPainter * painter,
int numItems,
QGraphicsItem * items[],
const QStyleOptionGraphicsItem options[],
QWidget * widget = nullptr )
protectedvirtual
Deprecated

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

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

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

Example:

const QStyleOptionGraphicsItem options[],
{
for (int i = 0; i < numItems; ++i) {
// Draw the item
items[i]->paint(painter, &options[i], widget);
}
}
void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[], QWidget *widget=nullptr) override
[0]
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
Adds or moves the item and all its children to this scene.
void restore()
Restores the current painter state (pops a saved state off the stack).
void save()
Saves the current painter state (pushes the state onto a stack).
void setTransform(const QTransform &transform, bool combine=false)
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
rect sceneTransform().map(QPointF(0
QList< QTreeWidgetItem * > items

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

See also
drawBackground(), drawForeground()
Deprecated

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

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

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

Example:

const QStyleOptionGraphicsItem options[],
{
for (int i = 0; i < numItems; ++i) {
// Draw the item
items[i]->paint(painter, &options[i], widget);
}
}
void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[], QWidget *widget=nullptr) override
[0]
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
Adds or moves the item and all its children to this scene.
void restore()
Restores the current painter state (pops a saved state off the stack).
void save()
Saves the current painter state (pushes the state onto a stack).
void setTransform(const QTransform &transform, bool combine=false)
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
rect sceneTransform().map(QPointF(0
QList< QTreeWidgetItem * > items

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

See also
drawBackground(), drawForeground()

Reimplemented in CustomScene, and CustomScene.

Definition at line 5263 of file qgraphicsscene.cpp.

◆ dropEvent()

void QGraphicsScene::dropEvent ( QGraphicsSceneDragDropEvent * event)
protectedvirtual

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

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

Definition at line 3629 of file qgraphicsscene.cpp.

◆ event()

bool QGraphicsScene::event ( QEvent * event)
overrideprotectedvirtual

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

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

Unlike QWidget, QGraphicsScene does not have the convenience functions \l{QWidget::}{enterEvent()} and \l{QWidget::}{leaveEvent()}. Use this function to obtain those events instead.

Returns true if event has been recognized and processed; otherwise, returns false.

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

Reimplemented from QObject.

Definition at line 3232 of file qgraphicsscene.cpp.

◆ eventFilter()

bool QGraphicsScene::eventFilter ( QObject * watched,
QEvent * event )
overrideprotectedvirtual

\reimp

QGraphicsScene filters QApplication's events to detect palette and font changes.

Reimplemented from QObject.

Definition at line 3449 of file qgraphicsscene.cpp.

◆ focusInEvent()

void QGraphicsScene::focusInEvent ( QFocusEvent * focusEvent)
protectedvirtual

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

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

See also
QGraphicsItem::focusOutEvent()

Definition at line 3649 of file qgraphicsscene.cpp.

◆ focusItem()

QGraphicsItem * QGraphicsScene::focusItem ( ) const

When the scene is active, this functions returns the scene's current focus item, or \nullptr if no item currently has focus.

When the scene is inactive, this functions returns the item that will gain input focus when the scene becomes active.

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

See also
setFocusItem(), QGraphicsItem::hasFocus(), isActive()

Definition at line 2855 of file qgraphicsscene.cpp.

◆ focusItemChanged

void QGraphicsScene::focusItemChanged ( QGraphicsItem * newFocusItem,
QGraphicsItem * oldFocusItem,
Qt::FocusReason reason )
signal

This signal is emitted by QGraphicsScene whenever focus changes in the scene (i.e., when an item gains or loses input focus, or when focus passes from one item to another).

You can connect to this signal if you need to keep track of when other items gain input focus. It is particularly useful for implementing virtual keyboards, input methods, and cursor items.

oldFocusItem is a pointer to the item that previously had focus, or 0 if no item had focus before the signal was emitted. newFocusItem is a pointer to the item that gained input focus, or \nullptr if focus was lost. reason is the reason for the focus change (e.g., if the scene was deactivated while an input field had focus, oldFocusItem would point to the input field item, newFocusItem would be \nullptr, and reason would be Qt::ActiveWindowFocusReason.

◆ focusNextPrevChild

bool QGraphicsScene::focusNextPrevChild ( bool next)
protectedvirtualslot
Since
4.4

Finds a new widget to give the keyboard focus to, as appropriate for Tab and Shift+Tab, and returns true if it can find a new widget, or false if it cannot. If next is true, this function searches forward; if next is false, it searches backward.

You can reimplement this function in a subclass of QGraphicsScene to provide fine-grained control over how tab focus passes inside your scene. The default implementation is based on the tab focus chain defined by QGraphicsWidget::setTabOrder().

Definition at line 5323 of file qgraphicsscene.cpp.

◆ focusOnTouch()

bool QGraphicsScene::focusOnTouch ( ) const

Definition at line 5780 of file qgraphicsscene.cpp.

◆ focusOutEvent()

void QGraphicsScene::focusOutEvent ( QFocusEvent * focusEvent)
protectedvirtual

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

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

See also
QGraphicsItem::focusInEvent()

Definition at line 3681 of file qgraphicsscene.cpp.

◆ font()

QFont QGraphicsScene::font ( ) const

Definition at line 5536 of file qgraphicsscene.cpp.

◆ foregroundBrush()

QBrush QGraphicsScene::foregroundBrush ( ) const

Definition at line 3046 of file qgraphicsscene.cpp.

◆ hasFocus()

bool QGraphicsScene::hasFocus ( ) const

Returns true if the scene has focus; otherwise returns false.

If the scene has focus, it will forward key events from QKeyEvent to any item that has focus.

See also
setFocus(), setFocusItem()

Definition at line 2893 of file qgraphicsscene.cpp.

◆ height()

qreal QGraphicsScene::height ( ) const
inline

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

See also
width()

Definition at line 93 of file qgraphicsscene.h.

◆ helpEvent()

void QGraphicsScene::helpEvent ( QGraphicsSceneHelpEvent * helpEvent)
protectedvirtual

This event handler, for event helpEvent, can be reimplemented in a subclass to receive help events.

The events are of type QEvent::ToolTip, which are created when a tooltip is requested.

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

Note: See items() for a definition of which items are considered visible by this function.

See also
QGraphicsItem::toolTip(), QGraphicsSceneHelpEvent

Definition at line 3708 of file qgraphicsscene.cpp.

◆ inputMethodEvent()

void QGraphicsScene::inputMethodEvent ( QInputMethodEvent * event)
protectedvirtual

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

The default implementation forwards the event to the focusItem(). If no item currently has focus or the current focus item does not accept input methods, this function does nothing.

See also
QGraphicsItem::inputMethodEvent()

Definition at line 4119 of file qgraphicsscene.cpp.

◆ inputMethodQuery()

QVariant QGraphicsScene::inputMethodQuery ( Qt::InputMethodQuery query) const
virtual

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

The query parameter specifies which property is queried.

See also
QWidget::inputMethodQuery()

Definition at line 3070 of file qgraphicsscene.cpp.

◆ invalidate [1/2]

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

Invalidates and schedules a redraw of the layers in rect on the scene.

Any cached content in layers is unconditionally invalidated and redrawn.

You can use this function overload to notify QGraphicsScene of changes to the background or the foreground of the scene. This function is commonly used for scenes with tile-based backgrounds to notify changes when QGraphicsView has enabled \l{QGraphicsView::CacheBackground}{CacheBackground}.

Example:

QRectF TileScene::rectForTile(int x, int y) const
{
// Return the rectangle for the tile at position (x, y).
return QRectF(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
}
void TileScene::setTile(int x, int y, const QPixmap &pixmap)
{
// Sets or replaces the tile at position (x, y) with pixmap.
if (x >= 0 && x < numTilesH && y >= 0 && y < numTilesV) {
tiles[y][x] = pixmap;
invalidate(rectForTile(x, y), BackgroundLayer);
}
}
void TileScene::drawBackground(QPainter *painter, const QRectF &exposed)
{
// Draws all tiles that intersect the exposed area.
for (int y = 0; y < numTilesV; ++y) {
for (int x = 0; x < numTilesH; ++x) {
QRectF rect = rectForTile(x, y);
if (exposed.intersects(rect))
painter->drawPixmap(rect.topLeft(), tiles[y][x]);
}
}
}
@ RepeatSpread
Definition qbrush.h:149
void setForegroundBrush(const QBrush &brush)
void setBackgroundBrush(const QBrush &brush)
QGraphicsTextItem * addText(const QString &text, const QFont &font=QFont())
Creates and adds a text item to the scene, and returns the item pointer.
void render(QPainter *painter, const QRectF &target=QRectF(), const QRectF &source=QRectF(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
Renders the source rect from scene into target, using painter.
\reentrant
Definition qprinter.h:28
@ HighResolution
Definition qprinter.h:31
bool intersects(const QRectF &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e.
Definition qrect.cpp:2267
\inmodule QtCore
Definition qsize.h:208
@ lightGray
Definition qnamespace.h:35
@ CrossPattern
GLint GLenum GLsizei GLsizei GLsizei depth

Note that QGraphicsView currently supports background caching only (see QGraphicsView::CacheBackground). This function is equivalent to calling update() if any layer but BackgroundLayer is passed.

See also
QGraphicsView::resetCachedContent()

Definition at line 3161 of file qgraphicsscene.cpp.

◆ invalidate() [2/2]

void QGraphicsScene::invalidate ( qreal x,
qreal y,
qreal w,
qreal h,
SceneLayers layers = AllLayers )
inline

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

Since
4.3

This convenience function is equivalent to calling invalidate(QRectF(x, y, w, h), layers);

Definition at line 174 of file qgraphicsscene.h.

◆ isActive()

bool QGraphicsScene::isActive ( ) const
Since
4.6

Returns true if the scene is active (e.g., it's viewed by at least one QGraphicsView that is active); otherwise returns false.

See also
QGraphicsItem::isActive(), QWidget::isActiveWindow()

Definition at line 5595 of file qgraphicsscene.cpp.

◆ itemAt() [1/2]

QGraphicsItem * QGraphicsScene::itemAt ( const QPointF & position,
const QTransform & deviceTransform ) const
Since
4.6

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

Note: See items() for a definition of which items are considered visible by this function.

See also
items(), collidingItems(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 2099 of file qgraphicsscene.cpp.

◆ itemAt() [2/2]

QGraphicsItem * QGraphicsScene::itemAt ( qreal x,
qreal y,
const QTransform & deviceTransform ) const
inline

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

Since
4.6

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

This convenience function is equivalent to calling {itemAt(QPointF(x, y), deviceTransform)}.

Note: See items() for a definition of which items are considered visible by this function.

Definition at line 122 of file qgraphicsscene.h.

◆ itemIndexMethod()

QGraphicsScene::ItemIndexMethod QGraphicsScene::itemIndexMethod ( ) const

Definition at line 1832 of file qgraphicsscene.cpp.

◆ items() [1/6]

QList< QGraphicsItem * > QGraphicsScene::items ( const QPainterPath & path,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape,
Qt::SortOrder order = Qt::DescendingOrder,
const QTransform & deviceTransform = QTransform() ) const

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

Since
4.6

Returns all visible items that, depending on mode, are either inside or intersect with the specified path, in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

See also
itemAt(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 2048 of file qgraphicsscene.cpp.

◆ items() [2/6]

QList< QGraphicsItem * > QGraphicsScene::items ( const QPointF & pos,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape,
Qt::SortOrder order = Qt::DescendingOrder,
const QTransform & deviceTransform = QTransform() ) const

Returns all visible items that, depending on mode, are at the specified pos in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.

Since
4.6

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

See also
itemAt(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 1970 of file qgraphicsscene.cpp.

◆ items() [3/6]

QList< QGraphicsItem * > QGraphicsScene::items ( const QPolygonF & polygon,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape,
Qt::SortOrder order = Qt::DescendingOrder,
const QTransform & deviceTransform = QTransform() ) const

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

Since
4.6

Returns all visible items that, depending on mode, are either inside or intersect with the specified polygon, in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

See also
itemAt(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 2022 of file qgraphicsscene.cpp.

◆ items() [4/6]

QList< QGraphicsItem * > QGraphicsScene::items ( const QRectF & rect,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape,
Qt::SortOrder order = Qt::DescendingOrder,
const QTransform & deviceTransform = QTransform() ) const

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

Since
4.6

Returns all visible items that, depending on mode, are either inside or intersect with the specified rect, in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.

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

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

See also
itemAt(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 1996 of file qgraphicsscene.cpp.

◆ items() [5/6]

QList< QGraphicsItem * > QGraphicsScene::items ( qreal x,
qreal y,
qreal w,
qreal h,
Qt::ItemSelectionMode mode,
Qt::SortOrder order,
const QTransform & deviceTransform = QTransform() ) const
inline

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

Since
4.6

Returns all visible items that, depending on mode, are either inside or intersect with the rectangle defined by x, y, w and h, in a list sorted using order. In this case, "visible" defines items for which: isVisible() returns true, effectiveOpacity() returns a value greater than 0.0 (which is fully transparent) and the parent item does not clip it.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

Definition at line 116 of file qgraphicsscene.h.

◆ items() [6/6]

QList< QGraphicsItem * > QGraphicsScene::items ( Qt::SortOrder order = Qt::DescendingOrder) const

Returns an ordered list of all items on the scene.

order decides the stacking order.

See also
addItem(), removeItem(), {QGraphicsItem#Sorting}{Sorting}

Definition at line 1932 of file qgraphicsscene.cpp.

◆ itemsBoundingRect()

QRectF QGraphicsScene::itemsBoundingRect ( ) const

Calculates and returns the bounding rect of all items on the scene.

This function works by iterating over all items, and because of this, it can be slow for large scenes.

See also
sceneRect()

Definition at line 1916 of file qgraphicsscene.cpp.

◆ keyPressEvent()

void QGraphicsScene::keyPressEvent ( QKeyEvent * keyEvent)
protectedvirtual

This event handler, for event keyEvent, can be reimplemented in a subclass to receive keypress events.

The default implementation forwards the event to current focus item.

See also
QGraphicsItem::keyPressEvent(), focusItem()

Definition at line 3871 of file qgraphicsscene.cpp.

◆ keyReleaseEvent()

void QGraphicsScene::keyReleaseEvent ( QKeyEvent * keyEvent)
protectedvirtual

This event handler, for event keyEvent, can be reimplemented in a subclass to receive key release events.

The default implementation forwards the event to current focus item.

See also
QGraphicsItem::keyReleaseEvent(), focusItem()

Definition at line 3903 of file qgraphicsscene.cpp.

◆ minimumRenderSize()

qreal QGraphicsScene::minimumRenderSize ( ) const

Definition at line 5745 of file qgraphicsscene.cpp.

◆ mouseDoubleClickEvent()

void QGraphicsScene::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * mouseEvent)
protectedvirtual

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

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

The default implementation is similar to mousePressEvent().

Note: See items() for a definition of which items are considered visible by this function.

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

Definition at line 4050 of file qgraphicsscene.cpp.

◆ mouseGrabberItem()

QGraphicsItem * QGraphicsScene::mouseGrabberItem ( ) const

Returns the current mouse grabber item, or \nullptr if no item is currently grabbing the mouse.

The mouse grabber item is the item that receives all mouse events sent to the scene.

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

\list

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

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

Definition at line 2988 of file qgraphicsscene.cpp.

◆ mouseMoveEvent()

void QGraphicsScene::mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent)
protectedvirtual

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

The default implementation depends on the mouse grabber state. If there is a mouse grabber item, the event is sent to the mouse grabber. If there are any items that accept hover events at the current position, the event is translated into a hover event and accepted; otherwise it's ignored.

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

Definition at line 3972 of file qgraphicsscene.cpp.

◆ mousePressEvent()

void QGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent)
protectedvirtual

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

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

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

Note: See items() for a definition of which items are considered visible by this function.

See also
QGraphicsItem::mousePressEvent(), QGraphicsItem::setAcceptedMouseButtons()

Definition at line 3947 of file qgraphicsscene.cpp.

◆ mouseReleaseEvent()

void QGraphicsScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent)
protectedvirtual

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

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

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

Definition at line 4002 of file qgraphicsscene.cpp.

◆ palette()

QPalette QGraphicsScene::palette ( ) const

Definition at line 5573 of file qgraphicsscene.cpp.

◆ removeItem()

void QGraphicsScene::removeItem ( QGraphicsItem * item)

Removes the item item and all its children from the scene.

The ownership of item is passed on to the caller (i.e., QGraphicsScene will no longer delete item when destroyed).

See also
addItem()

Definition at line 2811 of file qgraphicsscene.cpp.

◆ render()

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

Renders the source rect from scene into target, using painter.

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

...
printer.setPaperSize(QPrinter::A4);
QPainter painter(&printer);

If source is a null rect, this function will use sceneRect() to determine what to render. If target is a null rect, the dimensions of painter's paint device will be used.

The source rect contents will be transformed according to aspectRatioMode to fit into the target rect. By default, the aspect ratio is kept, and source is scaled to fit in target.

See also
QGraphicsView::render()

Definition at line 1745 of file qgraphicsscene.cpp.

◆ sceneRect()

QRectF QGraphicsScene::sceneRect ( ) const

Definition at line 1684 of file qgraphicsscene.cpp.

◆ sceneRectChanged

void QGraphicsScene::sceneRectChanged ( const QRectF & rect)
signal

This signal is emitted by QGraphicsScene whenever the scene rect changes.

The rect parameter is the new scene rectangle.

See also
QGraphicsView::updateSceneRect()

◆ selectedItems()

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

Returns a list of all currently selected items.

The items are returned in no particular order.

See also
setSelectionArea()

Definition at line 2129 of file qgraphicsscene.cpp.

◆ selectionArea()

QPainterPath QGraphicsScene::selectionArea ( ) const

Returns the selection area that was previously set with setSelectionArea(), or an empty QPainterPath if no selection area has been set.

See also
setSelectionArea()

Definition at line 2153 of file qgraphicsscene.cpp.

◆ selectionChanged

void QGraphicsScene::selectionChanged ( )
signal
Since
4.3

This signal is emitted by QGraphicsScene whenever the selection changes. You can call selectedItems() to get the new list of selected items.

The selection changes whenever an item is selected or unselected, a selection area is set, cleared or otherwise changed, if a preselected item is added to the scene, or if a selected item is removed from the scene.

QGraphicsScene emits this signal only once for group selection operations. For example, if you set a selection area, select or unselect a QGraphicsItemGroup, or if you add or remove from the scene a parent item that contains several selected items, selectionChanged() is emitted only once after the operation has completed (instead of once for each item).

See also
setSelectionArea(), selectedItems(), QGraphicsItem::setSelected()

◆ sendEvent()

bool QGraphicsScene::sendEvent ( QGraphicsItem * item,
QEvent * event )
Since
4.6

Sends event event to item item through possible event filters.

The event is sent only if the item is enabled.

Returns false if the event was filtered or if the item is disabled. Otherwise returns the value that was returned from the event handler.

See also
QGraphicsItem::sceneEvent(), QGraphicsItem::sceneEventFilter()

Definition at line 5698 of file qgraphicsscene.cpp.

◆ setActivePanel()

void QGraphicsScene::setActivePanel ( QGraphicsItem * item)
Since
4.6 Activates item, which must be an item in this scene. You can also pass 0 for item, in which case QGraphicsScene will deactivate any currently active panel.

If the scene is currently inactive, item remains inactive until the scene becomes active (or, ir item is \nullptr, no item will be activated).

See also
activePanel(), isActive(), QGraphicsItem::isActive()

Definition at line 5625 of file qgraphicsscene.cpp.

◆ setActiveWindow()

void QGraphicsScene::setActiveWindow ( QGraphicsWidget * widget)
Since
4.4 Activates widget, which must be a widget in this scene. You can also pass 0 for widget, in which case QGraphicsScene will deactivate any currently active window.
See also
activeWindow(), QGraphicsWidget::isActiveWindow()

Definition at line 5655 of file qgraphicsscene.cpp.

◆ setBackgroundBrush()

void QGraphicsScene::setBackgroundBrush ( const QBrush & brush)

Definition at line 3015 of file qgraphicsscene.cpp.

◆ setBspTreeDepth()

void QGraphicsScene::setBspTreeDepth ( int depth)

Definition at line 1893 of file qgraphicsscene.cpp.

◆ setFocus()

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

Sets focus on the scene by sending a QFocusEvent to the scene, passing focusReason as the reason.

If the scene regains focus after having previously lost it while an item had focus, the last focus item will receive focus with focusReason as the reason.

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

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

Definition at line 2909 of file qgraphicsscene.cpp.

◆ setFocusItem()

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

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

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

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

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

Definition at line 2877 of file qgraphicsscene.cpp.

◆ setFocusOnTouch()

void QGraphicsScene::setFocusOnTouch ( bool enabled)

Definition at line 5786 of file qgraphicsscene.cpp.

◆ setFont()

void QGraphicsScene::setFont ( const QFont & font)

Definition at line 5541 of file qgraphicsscene.cpp.

◆ setForegroundBrush()

void QGraphicsScene::setForegroundBrush ( const QBrush & brush)

Definition at line 3051 of file qgraphicsscene.cpp.

◆ setItemIndexMethod()

void QGraphicsScene::setItemIndexMethod ( ItemIndexMethod method)

Definition at line 1837 of file qgraphicsscene.cpp.

◆ setMinimumRenderSize()

void QGraphicsScene::setMinimumRenderSize ( qreal minSize)

Definition at line 5750 of file qgraphicsscene.cpp.

◆ setPalette()

void QGraphicsScene::setPalette ( const QPalette & palette)

Definition at line 5578 of file qgraphicsscene.cpp.

◆ setSceneRect() [1/2]

void QGraphicsScene::setSceneRect ( const QRectF & rect)

Definition at line 1701 of file qgraphicsscene.cpp.

◆ setSceneRect() [2/2]

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

Definition at line 95 of file qgraphicsscene.h.

◆ setSelectionArea() [1/2]

void QGraphicsScene::setSelectionArea ( const QPainterPath & path,
const QTransform & deviceTransform )
Since
4.6

Sets the selection area to path. All items within this area are immediately selected, and all items outside are unselected. You can get the list of all selected items by calling selectedItems().

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

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

See also
clearSelection(), selectionArea()

Definition at line 2174 of file qgraphicsscene.cpp.

◆ setSelectionArea() [2/2]

void QGraphicsScene::setSelectionArea ( const QPainterPath & path,
Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection,
Qt::ItemSelectionMode mode = Qt::IntersectsItemShape,
const QTransform & deviceTransform = QTransform() )

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

Since
5.5

Sets the selection area to path using mode to determine if items are included in the selection area.

deviceTransform is the transformation that applies to the view, and needs to be provided if the scene contains items that ignore transformations.

selectionOperation determines what to do with the currently selected items.

See also
clearSelection(), selectionArea()

Definition at line 2193 of file qgraphicsscene.cpp.

◆ setStickyFocus()

void QGraphicsScene::setStickyFocus ( bool enabled)

Definition at line 2955 of file qgraphicsscene.cpp.

◆ setStyle()

void QGraphicsScene::setStyle ( QStyle * style)
Since
4.4

Sets or replaces the style of the scene to style, and reparents the style to this scene. Any previously assigned style is deleted. The scene's style defaults to QApplication::style(), and serves as the default for all QGraphicsWidget items in the scene.

Changing the style, either directly by calling this function, or indirectly by calling QApplication::setStyle(), will automatically update the style for all widgets in the scene that do not have a style explicitly assigned to them.

If style is \nullptr, QGraphicsScene will revert to QApplication::style().

See also
style()

Definition at line 5486 of file qgraphicsscene.cpp.

◆ stickyFocus()

bool QGraphicsScene::stickyFocus ( ) const

Definition at line 2960 of file qgraphicsscene.cpp.

◆ style()

QStyle * QGraphicsScene::style ( ) const
Since
4.4

Returns the scene's style, or the same as QApplication::style() if the scene has not been explicitly assigned a style.

See also
setStyle()

Definition at line 5462 of file qgraphicsscene.cpp.

◆ update [1/2]

void QGraphicsScene::update ( const QRectF & rect = QRectF())
slot

Schedules a redraw of the area rect on the scene.

See also
sceneRect(), changed()

Definition at line 3094 of file qgraphicsscene.cpp.

◆ update() [2/2]

void QGraphicsScene::update ( qreal x,
qreal y,
qreal w,
qreal h )
inline

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

Since
4.3

This function is equivalent to calling update(QRectF(x, y, w, h));

Definition at line 172 of file qgraphicsscene.h.

◆ views()

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

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

See also
QGraphicsView::scene()

Definition at line 3183 of file qgraphicsscene.cpp.

◆ wheelEvent()

void QGraphicsScene::wheelEvent ( QGraphicsSceneWheelEvent * wheelEvent)
protectedvirtual

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

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

Note: See items() for a definition of which items are considered visible by this function.

See also
QGraphicsItem::wheelEvent()

Definition at line 4069 of file qgraphicsscene.cpp.

◆ width()

qreal QGraphicsScene::width ( ) const
inline

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

See also
height()

Definition at line 92 of file qgraphicsscene.h.

Friends And Related Symbol Documentation

◆ QGesture

friend class QGesture
friend

Definition at line 264 of file qgraphicsscene.h.

◆ QGraphicsEffect

friend class QGraphicsEffect
friend

Definition at line 257 of file qgraphicsscene.h.

◆ QGraphicsItemEffectSourcePrivate

friend class QGraphicsItemEffectSourcePrivate
friend

Definition at line 262 of file qgraphicsscene.h.

◆ QGraphicsItemPrivate

friend class QGraphicsItemPrivate
friend

Definition at line 251 of file qgraphicsscene.h.

◆ QGraphicsObject

friend class QGraphicsObject
friend

Definition at line 252 of file qgraphicsscene.h.

◆ QGraphicsSceneBspTreeIndex

friend class QGraphicsSceneBspTreeIndex
friend

Definition at line 260 of file qgraphicsscene.h.

◆ QGraphicsSceneBspTreeIndexPrivate

friend class QGraphicsSceneBspTreeIndexPrivate
friend

Definition at line 261 of file qgraphicsscene.h.

◆ QGraphicsSceneIndex

friend class QGraphicsSceneIndex
friend

Definition at line 258 of file qgraphicsscene.h.

◆ QGraphicsSceneIndexPrivate

friend class QGraphicsSceneIndexPrivate
friend

Definition at line 259 of file qgraphicsscene.h.

◆ QGraphicsView

friend class QGraphicsView
friend

Definition at line 253 of file qgraphicsscene.h.

◆ QGraphicsViewPrivate

friend class QGraphicsViewPrivate
friend

Definition at line 254 of file qgraphicsscene.h.

◆ QGraphicsWidget

friend class QGraphicsWidget
friend

Definition at line 255 of file qgraphicsscene.h.

◆ QGraphicsWidgetPrivate

friend class QGraphicsWidgetPrivate
friend

Definition at line 256 of file qgraphicsscene.h.

Property Documentation

◆ backgroundBrush

QBrush QGraphicsScene::backgroundBrush
readwrite

the background brush of the scene.

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

Example:

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

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

Definition at line 61 of file qgraphicsscene.h.

◆ bspTreeDepth

int QGraphicsScene::bspTreeDepth
readwrite

the depth of QGraphicsScene's BSP index tree

Since
4.3

This property has no effect when NoIndex is used.

This value determines the depth of QGraphicsScene's BSP tree. The depth directly affects QGraphicsScene's performance and memory usage; the latter growing exponentially with the depth of the tree. With an optimal tree depth, QGraphicsScene can instantly determine the locality of items, even for scenes with thousands or millions of items. This also greatly improves rendering performance.

By default, the value is 0, in which case Qt will guess a reasonable default depth based on the size, location and number of items in the scene. If these parameters change frequently, however, you may experience slowdowns as QGraphicsScene retunes the depth internally. You can avoid potential slowdowns by fixating the tree depth through setting this property.

The depth of the tree and the size of the scene rectangle decide the granularity of the scene's partitioning. The size of each scene segment is determined by the following algorithm:

QSizeF segmentSize = sceneRect().size() / pow(2, depth - 1);

The BSP tree has an optimal size when each segment contains between 0 and 10 items.

See also
itemIndexMethod

Definition at line 65 of file qgraphicsscene.h.

◆ focusOnTouch

bool QGraphicsScene::focusOnTouch
readwrite

whether items gain focus when receiving a {touch begin} event.

Since
5.12

The usual behavior is to transfer focus only when an item is clicked. Often a tap on a touchpad is interpreted as equivalent to a mouse click by the operating system, generating a synthesized click event in response. However, at least on macOS you can configure this behavior.

By default, QGraphicsScene also transfers focus when you touch on a trackpad or similar. If the operating system is configured to not generate a synthetic mouse click on tapping the trackpad, this is surprising. If the operating system does generate synthetic mouse clicks on tapping the trackpad, the focus transfer on starting a touch gesture is unnecessary.

With focusOnTouch switched off, QGraphicsScene behaves as one would expect on macOS.

The default value is true, ensuring that the default behavior is just as in Qt versions prior to 5.12. Set to false to prevent touch events from triggering focus changes.

Definition at line 70 of file qgraphicsscene.h.

◆ font

QFont QGraphicsScene::font
readwrite

the scene's default font

Since
4.4

This property provides the scene's font. The scene font defaults to, and resolves all its entries from, QApplication::font.

If the scene's font changes, either directly through setFont() or indirectly when the application font changes, QGraphicsScene first sends itself a \l{QEvent::FontChange}{FontChange} event, and it then sends \l{QEvent::FontChange}{FontChange} events to all top-level widget items in the scene. These items respond by resolving their own fonts to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their fonts.

Changing the scene font, (directly or indirectly through QApplication::setFont(),) automatically schedules a redraw the entire scene.

See also
QWidget::font, QApplication::setFont(), palette, style()

Definition at line 67 of file qgraphicsscene.h.

◆ foregroundBrush

QBrush QGraphicsScene::foregroundBrush
readwrite

the foreground brush of the scene.

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

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

Example:

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

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

Definition at line 62 of file qgraphicsscene.h.

◆ itemIndexMethod

ItemIndexMethod QGraphicsScene::itemIndexMethod
readwrite

the item indexing method.

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

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

See also
bspTreeDepth

Definition at line 63 of file qgraphicsscene.h.

◆ minimumRenderSize

qreal QGraphicsScene::minimumRenderSize
readwrite

the minimal view-transformed size an item must have to be drawn

Since
5.4

When the scene is rendered, any item whose width or height, transformed to the target view, is smaller that minimumRenderSize(), will not be rendered. If an item is not rendered and it clips its children items they will also not be rendered. Set this value to speed up rendering of scenes with many objects rendered on a zoomed out view.

The default value is 0. If unset, or if set to 0 or a negative value, all items will always be rendered.

For example, setting this property can be especially useful if a scene is rendered by multiple views, one of which serves as an overview which always displays all items. In scenes with many items, such a view will use a high scaling factor so that all items can be shown. Due to the scaling, smaller items will only make an insignificant contribution to the final rendered scene. To avoid drawing these items and reduce the time necessary to render the scene, you can call setMinimumRenderSize() with a non-negative value.

Note
Items that are not drawn as a result of being too small, are still returned by methods such as items() and itemAt(), and participate in collision detection and interactions. It is recommended that you set minimumRenderSize() to a value less than or equal to 1 in order to avoid large unrendered items that are interactive.
See also
QStyleOptionGraphicsItem::levelOfDetailFromTransform()

Definition at line 69 of file qgraphicsscene.h.

◆ palette

QPalette QGraphicsScene::palette
readwrite

the scene's default palette

Since
4.4

This property provides the scene's palette. The scene palette defaults to, and resolves all its entries from, QApplication::palette.

If the scene's palette changes, either directly through setPalette() or indirectly when the application palette changes, QGraphicsScene first sends itself a \l{QEvent::PaletteChange}{PaletteChange} event, and it then sends \l{QEvent::PaletteChange}{PaletteChange} events to all top-level widget items in the scene. These items respond by resolving their own palettes to the scene, and they then notify their children, who again notify their children, and so on, until all widget items have updated their palettes.

Changing the scene palette, (directly or indirectly through QApplication::setPalette(),) automatically schedules a redraw the entire scene.

See also
QWidget::palette, QApplication::setPalette(), font, style()

Definition at line 66 of file qgraphicsscene.h.

◆ sceneRect

QRectF QGraphicsScene::sceneRect
readwrite

the scene rectangle; the bounding rectangle of the scene

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

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

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

Definition at line 64 of file qgraphicsscene.h.

◆ stickyFocus

bool QGraphicsScene::stickyFocus
readwrite

whether clicking into the scene background will clear focus

Since
4.6

In a QGraphicsScene with stickyFocus set to true, focus will remain unchanged when the user clicks into the scene background or on an item that does not accept focus. Otherwise, focus will be cleared.

By default, this property is false.

Focus changes in response to a mouse press. You can reimplement mousePressEvent() in a subclass of QGraphicsScene to toggle this property based on where the user has clicked.

See also
clearFocus(), setFocusItem()

Definition at line 68 of file qgraphicsscene.h.


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