Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgraphicsscene.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGRAPHICSSCENE_H
5#define QGRAPHICSSCENE_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qobject.h>
9#include <QtCore/qpoint.h>
10#include <QtCore/qrect.h>
11#include <QtGui/qbrush.h>
12#include <QtGui/qfont.h>
13#include <QtGui/qtransform.h>
14#include <QtGui/qpen.h>
15
16QT_REQUIRE_CONFIG(graphicsview);
17
19
20class QFocusEvent;
21class QFont;
22class QFontMetrics;
24class QGraphicsItem;
41class QGraphicsView;
42class QGraphicsWidget;
44class QHelpEvent;
46class QKeyEvent;
47class QLineF;
48class QPainterPath;
49class QPixmap;
50class QPointF;
51class QPolygonF;
52class QRectF;
53class QSizeF;
54class QStyle;
56
58class Q_WIDGETS_EXPORT QGraphicsScene : public QObject
59{
61 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
62 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
63 Q_PROPERTY(ItemIndexMethod itemIndexMethod READ itemIndexMethod WRITE setItemIndexMethod)
64 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
65 Q_PROPERTY(int bspTreeDepth READ bspTreeDepth WRITE setBspTreeDepth)
66 Q_PROPERTY(QPalette palette READ palette WRITE setPalette)
67 Q_PROPERTY(QFont font READ font WRITE setFont)
68 Q_PROPERTY(bool stickyFocus READ stickyFocus WRITE setStickyFocus)
69 Q_PROPERTY(qreal minimumRenderSize READ minimumRenderSize WRITE setMinimumRenderSize)
70 Q_PROPERTY(bool focusOnTouch READ focusOnTouch WRITE setFocusOnTouch)
71
72public:
75 NoIndex = -1
76 };
77 Q_ENUM(ItemIndexMethod)
79 ItemLayer = 0x1,
80 BackgroundLayer = 0x2,
81 ForegroundLayer = 0x4,
82 AllLayers = 0xffff
83 };
84 Q_DECLARE_FLAGS(SceneLayers, SceneLayer)
85
86 QGraphicsScene(QObject *parent = nullptr);
87 QGraphicsScene(const QRectF &sceneRect, QObject *parent = nullptr);
88 QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = nullptr);
89 virtual ~QGraphicsScene();
90
91 QRectF sceneRect() const;
92 inline qreal width() const { return sceneRect().width(); }
93 inline qreal height() const { return sceneRect().height(); }
94 void setSceneRect(const QRectF &rect);
96 { setSceneRect(QRectF(x, y, w, h)); }
97
99 const QRectF &target = QRectF(), const QRectF &source = QRectF(),
100 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
101
102 ItemIndexMethod itemIndexMethod() const;
103 void setItemIndexMethod(ItemIndexMethod method);
104
105 int bspTreeDepth() const;
106 void setBspTreeDepth(int depth);
107
108 QRectF itemsBoundingRect() const;
109
110 QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const;
111
117 const QTransform &deviceTransform = QTransform()) const
118 { return items(QRectF(x, y, w, h), mode, order, deviceTransform); }
119
120 QList<QGraphicsItem *> collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
121 QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
123 { return itemAt(QPointF(x, y), deviceTransform); }
124
125 QList<QGraphicsItem *> selectedItems() const;
126 QPainterPath selectionArea() const;
127 void setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform);
129
130 QGraphicsItemGroup *createItemGroup(const QList<QGraphicsItem *> &items);
132
134 QGraphicsEllipseItem *addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
135 QGraphicsLineItem *addLine(const QLineF &line, const QPen &pen = QPen());
136 QGraphicsPathItem *addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush());
137 QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap);
138 QGraphicsPolygonItem *addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush());
139 QGraphicsRectItem *addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
140 QGraphicsTextItem *addText(const QString &text, const QFont &font = QFont());
141 QGraphicsSimpleTextItem *addSimpleText(const QString &text, const QFont &font = QFont());
142 QGraphicsProxyWidget *addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags());
144 { return addEllipse(QRectF(x, y, w, h), pen, brush); }
146 { return addLine(QLineF(x1, y1, x2, y2), pen); }
147 inline QGraphicsRectItem *addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
148 { return addRect(QRectF(x, y, w, h), pen, brush); }
149 void removeItem(QGraphicsItem *item);
150
151 QGraphicsItem *focusItem() const;
152 void setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason);
153 bool hasFocus() const;
154 void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
155 void clearFocus();
156
157 void setStickyFocus(bool enabled);
158 bool stickyFocus() const;
159
160 QGraphicsItem *mouseGrabberItem() const;
161
162 QBrush backgroundBrush() const;
163 void setBackgroundBrush(const QBrush &brush);
164
165 QBrush foregroundBrush() const;
166 void setForegroundBrush(const QBrush &brush);
167
168 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
169
170 QList <QGraphicsView *> views() const;
171
172 inline void update(qreal x, qreal y, qreal w, qreal h)
173 { update(QRectF(x, y, w, h)); }
174 inline void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers = AllLayers)
175 { invalidate(QRectF(x, y, w, h), layers); }
176
177 QStyle *style() const;
178 void setStyle(QStyle *style);
179
180 QFont font() const;
181 void setFont(const QFont &font);
182
183 QPalette palette() const;
184 void setPalette(const QPalette &palette);
185
186 bool isActive() const;
187 QGraphicsItem *activePanel() const;
188 void setActivePanel(QGraphicsItem *item);
189 QGraphicsWidget *activeWindow() const;
190 void setActiveWindow(QGraphicsWidget *widget);
191
192 bool sendEvent(QGraphicsItem *item, QEvent *event);
193
194 qreal minimumRenderSize() const;
195 void setMinimumRenderSize(qreal minSize);
196
197 bool focusOnTouch() const;
198 void setFocusOnTouch(bool enabled);
199
200public Q_SLOTS:
201 void update(const QRectF &rect = QRectF());
202 void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers);
203 void advance();
204 void clearSelection();
205 void clear();
206
207protected:
208 bool event(QEvent *event) override;
209 bool eventFilter(QObject *watched, QEvent *event) override;
210 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
211 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
212 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
213 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
214 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
215 virtual void focusInEvent(QFocusEvent *event);
216 virtual void focusOutEvent(QFocusEvent *event);
217 virtual void helpEvent(QGraphicsSceneHelpEvent *event);
218 virtual void keyPressEvent(QKeyEvent *event);
219 virtual void keyReleaseEvent(QKeyEvent *event);
220 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
221 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
222 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
223 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
224 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
225 virtual void inputMethodEvent(QInputMethodEvent *event);
226
227 virtual void drawBackground(QPainter *painter, const QRectF &rect);
228 virtual void drawForeground(QPainter *painter, const QRectF &rect);
229 virtual void drawItems(QPainter *painter, int numItems,
231 const QStyleOptionGraphicsItem options[],
232 QWidget *widget = nullptr);
233
234protected Q_SLOTS:
235 virtual bool focusNextPrevChild(bool next);
236
238 void changed(const QList<QRectF> &region);
242
243private:
244 Q_DECLARE_PRIVATE(QGraphicsScene)
245 Q_DISABLE_COPY(QGraphicsScene)
246 Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated())
247 Q_PRIVATE_SLOT(d_func(), void _q_polishItems())
248 Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems())
249 Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants())
250 friend class QGraphicsItem;
252 friend class QGraphicsObject;
253 friend class QGraphicsView;
255 friend class QGraphicsWidget;
257 friend class QGraphicsEffect;
262 friend class QGraphicsItemEffectSourcePrivate;
263#ifndef QT_NO_GESTURES
264 friend class QGesture;
265#endif
266};
267
268Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
269
271
272#endif
bool isActive
\inmodule QtGui
Definition qbrush.h:30
\inmodule QtCore
Definition qcoreevent.h:45
The QFocusEvent class contains event parameters for widget focus events.
Definition qevent.h:470
\reentrant \inmodule QtGui
\reentrant
Definition qfont.h:22
The QGesture class represents a gesture, containing properties that describe the corresponding user i...
Definition qgesture.h:29
The QGraphicsEffect class is the base class for all graphics effects.
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
The QGraphicsItemGroup class provides a container that treats a group of items as a single item.
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
The QGraphicsObject class provides a base class for all graphics items that require signals,...
The QGraphicsPixmapItem class provides a pixmap item that you can add to a QGraphicsScene.
The QGraphicsPolygonItem class provides a polygon item that you can add to a QGraphicsScene.
The QGraphicsProxyWidget class provides a proxy layer for embedding a QWidget in a QGraphicsScene.
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsSceneBspTreeIndex class provides an implementation of a BSP indexing algorithm for disco...
The QGraphicsSceneContextMenuEvent class provides context menu events in the graphics view framework.
The QGraphicsSceneDragDropEvent class provides events for drag and drop in the graphics view framewor...
The QGraphicsSceneEvent class provides a base class for all graphics view related events.
The QGraphicsSceneHelpEvent class provides events when a tooltip is requested.
The QGraphicsSceneHoverEvent class provides hover events in the graphics view framework.
The QGraphicsSceneIndex class provides a base class to implement a custom indexing algorithm for disc...
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
The QGraphicsSceneWheelEvent class provides wheel events in the graphics view framework.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
QGraphicsEllipseItem * addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
void changed(const QList< QRectF > &region)
This signal is emitted by QGraphicsScene when control reaches the event loop, if the scene content ch...
qreal height() const
This convenience function is equivalent to calling sceneRect().height().
void setSceneRect(qreal x, qreal y, qreal w, qreal h)
qreal width() const
This convenience function is equivalent to calling sceneRect().width().
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 o...
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 o...
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 o...
void selectionChanged()
void sceneRectChanged(const QRectF &rect)
This signal is emitted by QGraphicsScene whenever the scene rect changes.
ItemIndexMethod
This enum describes the indexing algorithms QGraphicsScene provides for managing positional informati...
void focusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason)
This signal is emitted by QGraphicsScene whenever focus changes in the scene (i.e....
QGraphicsItem * itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QGraphicsRectItem * addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QGraphicsLineItem * addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen=QPen())
The QGraphicsSimpleTextItem class provides a simple text path item that you can add to a QGraphicsSce...
The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display form...
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
The QGraphicsWidget class is the base class for all widget items in a QGraphicsScene.
The QHelpEvent class provides an event that is used to request helpful information about a particular...
Definition qevent.h:788
The QInputMethodEvent class provides parameters for input method events.
Definition qevent.h:625
The QKeyEvent class describes a key event.
Definition qevent.h:424
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
\inmodule QtCore
Definition qobject.h:103
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
\inmodule QtGui
Definition qpen.h:28
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qpoint.h:217
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore
Definition qsize.h:208
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.
Definition qstyle.h:29
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
\inmodule QtCore
Definition qvariant.h:65
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QOpenGLWidget * widget
[1]
b clear()
QString text
rect
[4]
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
InputMethodQuery
AspectRatioMode
@ KeepAspectRatio
ItemSelectionOperation
@ ReplaceSelection
SortOrder
Definition qnamespace.h:121
@ DescendingOrder
Definition qnamespace.h:123
ItemSelectionMode
@ IntersectsItemShape
FocusReason
@ OtherFocusReason
Definition brush.cpp:5
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
const EGLAttrib EGLOutputLayerEXT * layers
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
GLint GLint GLint GLint GLint x
[0]
GLint GLenum GLsizei GLsizei GLsizei depth
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLuint GLfloat GLfloat GLfloat x1
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLint GLsizei width
GLboolean GLuint group
GLenum target
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLsizei GLchar * source
struct _cl_event * event
GLenum query
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
GLsizei const GLchar *const * path
GLfixed GLfixed GLint GLint order
#define QT_REQUIRE_CONFIG(feature)
static void drawBackground(QPainter *p, const QTextCharFormat &chf, const QRectF &r)
#define Q_ENUM(x)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SLOTS
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
double qreal
Definition qtypes.h:187
scene addItem(form)
QGraphicsItem * item
scene destroyItemGroup(group)
rect deviceTransform(view->viewportTransform()).map(QPointF(0
scene addText("Hello, world!")
view setBackgroundBrush(QImage(":/images/backgroundtile.png"))
scene addRect(QRectF(-10, -10, 20, 20))
QList< QTreeWidgetItem * > items
gridLayout addWidget(nameLabel, 0, 0)
widget render & pixmap
QPainter painter(this)
[7]
myWidget render(this)
widget setStyle(proxy)