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.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// Qt-Security score:significant reason:default
4
5#ifndef QGRAPHICSSCENE_H
6#define QGRAPHICSSCENE_H
7
8#include <QtWidgets/qtwidgetsglobal.h>
9#include <QtCore/qobject.h>
10#include <QtCore/qpoint.h>
11#include <QtCore/qrect.h>
12#include <QtGui/qbrush.h>
13#include <QtGui/qfont.h>
14#include <QtGui/qtransform.h>
15#include <QtGui/qpen.h>
16
17QT_REQUIRE_CONFIG(graphicsview);
18
19QT_BEGIN_NAMESPACE
20
21class QFocusEvent;
22class QFont;
23class QFontMetrics;
24class QGraphicsEllipseItem;
25class QGraphicsItem;
26class QGraphicsItemGroup;
27class QGraphicsLineItem;
29class QGraphicsPixmapItem;
30class QGraphicsPolygonItem;
31class QGraphicsProxyWidget;
32class QGraphicsRectItem;
33class QGraphicsSceneContextMenuEvent;
34class QGraphicsSceneDragDropEvent;
35class QGraphicsSceneEvent;
36class QGraphicsSceneHelpEvent;
37class QGraphicsSceneHoverEvent;
38class QGraphicsSceneMouseEvent;
39class QGraphicsSceneWheelEvent;
40class QGraphicsSimpleTextItem;
41class QGraphicsTextItem;
42class QGraphicsView;
43class QGraphicsWidget;
44class QGraphicsSceneIndex;
45class QHelpEvent;
46class QInputMethodEvent;
47class QKeyEvent;
48class QLineF;
49class QPainterPath;
50class QPixmap;
51class QPointF;
52class QPolygonF;
53class QRectF;
54class QSizeF;
55class QStyle;
56class QStyleOptionGraphicsItem;
57
58class QGraphicsScenePrivate;
59class Q_WIDGETS_EXPORT QGraphicsScene : public QObject
60{
61 Q_OBJECT
62 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
63 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
64 Q_PROPERTY(ItemIndexMethod itemIndexMethod READ itemIndexMethod WRITE setItemIndexMethod)
65 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
66 Q_PROPERTY(int bspTreeDepth READ bspTreeDepth WRITE setBspTreeDepth)
67 Q_PROPERTY(QPalette palette READ palette WRITE setPalette)
68 Q_PROPERTY(QFont font READ font WRITE setFont)
69 Q_PROPERTY(bool stickyFocus READ stickyFocus WRITE setStickyFocus)
70 Q_PROPERTY(qreal minimumRenderSize READ minimumRenderSize WRITE setMinimumRenderSize)
71 Q_PROPERTY(bool focusOnTouch READ focusOnTouch WRITE setFocusOnTouch)
72
73public:
74 enum ItemIndexMethod {
75 BspTreeIndex,
76 NoIndex = -1
77 };
78 Q_ENUM(ItemIndexMethod)
79 enum SceneLayer {
80 ItemLayer = 0x1,
81 BackgroundLayer = 0x2,
82 ForegroundLayer = 0x4,
83 AllLayers = 0xffff
84 };
85 Q_DECLARE_FLAGS(SceneLayers, SceneLayer)
86
87 QGraphicsScene(QObject *parent = nullptr);
88 QGraphicsScene(const QRectF &sceneRect, QObject *parent = nullptr);
89 QGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent = nullptr);
90 virtual ~QGraphicsScene();
91
92 QRectF sceneRect() const;
93 inline qreal width() const { return sceneRect().width(); }
94 inline qreal height() const { return sceneRect().height(); }
95 void setSceneRect(const QRectF &rect);
96 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h)
97 { setSceneRect(QRectF(x, y, w, h)); }
98
99 void render(QPainter *painter,
100 const QRectF &target = QRectF(), const QRectF &source = QRectF(),
101 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
102
103 ItemIndexMethod itemIndexMethod() const;
104 void setItemIndexMethod(ItemIndexMethod method);
105
106 int bspTreeDepth() const;
107 void setBspTreeDepth(int depth);
108
109 QRectF itemsBoundingRect() const;
110
111 QList<QGraphicsItem *> items(Qt::SortOrder order = Qt::DescendingOrder) const;
112
113 QList<QGraphicsItem *> items(const QPointF &pos, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
114 QList<QGraphicsItem *> items(const QRectF &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
115 QList<QGraphicsItem *> items(const QPolygonF &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
116 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, Qt::SortOrder order = Qt::DescendingOrder, const QTransform &deviceTransform = QTransform()) const;
117 inline QList<QGraphicsItem *> items(qreal x, qreal y, qreal w, qreal h, Qt::ItemSelectionMode mode, Qt::SortOrder order,
118 const QTransform &deviceTransform = QTransform()) const
119 { return items(QRectF(x, y, w, h), mode, order, deviceTransform); }
120
121 QList<QGraphicsItem *> collidingItems(const QGraphicsItem *item, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
122 QGraphicsItem *itemAt(const QPointF &pos, const QTransform &deviceTransform) const;
123 inline QGraphicsItem *itemAt(qreal x, qreal y, const QTransform &deviceTransform) const
124 { return itemAt(QPointF(x, y), deviceTransform); }
125
126 QList<QGraphicsItem *> selectedItems() const;
127 QPainterPath selectionArea() const;
128 void setSelectionArea(const QPainterPath &path, const QTransform &deviceTransform);
129 void setSelectionArea(const QPainterPath &path, Qt::ItemSelectionOperation selectionOperation = Qt::ReplaceSelection, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape, const QTransform &deviceTransform = QTransform());
130
131 QGraphicsItemGroup *createItemGroup(const QList<QGraphicsItem *> &items);
132 void destroyItemGroup(QGraphicsItemGroup *group);
133
134 void addItem(QGraphicsItem *item);
135 QGraphicsEllipseItem *addEllipse(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
136 QGraphicsLineItem *addLine(const QLineF &line, const QPen &pen = QPen());
137 QGraphicsPathItem *addPath(const QPainterPath &path, const QPen &pen = QPen(), const QBrush &brush = QBrush());
138 QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap);
139 QGraphicsPolygonItem *addPolygon(const QPolygonF &polygon, const QPen &pen = QPen(), const QBrush &brush = QBrush());
140 QGraphicsRectItem *addRect(const QRectF &rect, const QPen &pen = QPen(), const QBrush &brush = QBrush());
141 QGraphicsTextItem *addText(const QString &text, const QFont &font = QFont());
142 QGraphicsSimpleTextItem *addSimpleText(const QString &text, const QFont &font = QFont());
143 QGraphicsProxyWidget *addWidget(QWidget *widget, Qt::WindowFlags wFlags = Qt::WindowFlags());
144 inline QGraphicsEllipseItem *addEllipse(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
145 { return addEllipse(QRectF(x, y, w, h), pen, brush); }
146 inline QGraphicsLineItem *addLine(qreal x1, qreal y1, qreal x2, qreal y2, const QPen &pen = QPen())
147 { return addLine(QLineF(x1, y1, x2, y2), pen); }
148 inline QGraphicsRectItem *addRect(qreal x, qreal y, qreal w, qreal h, const QPen &pen = QPen(), const QBrush &brush = QBrush())
149 { return addRect(QRectF(x, y, w, h), pen, brush); }
150 void removeItem(QGraphicsItem *item);
151
152 QGraphicsItem *focusItem() const;
153 void setFocusItem(QGraphicsItem *item, Qt::FocusReason focusReason = Qt::OtherFocusReason);
154 bool hasFocus() const;
155 void setFocus(Qt::FocusReason focusReason = Qt::OtherFocusReason);
156 void clearFocus();
157
158 void setStickyFocus(bool enabled);
159 bool stickyFocus() const;
160
161 QGraphicsItem *mouseGrabberItem() const;
162
163 QBrush backgroundBrush() const;
164 void setBackgroundBrush(const QBrush &brush);
165
166 QBrush foregroundBrush() const;
167 void setForegroundBrush(const QBrush &brush);
168
169 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
170
171 QList <QGraphicsView *> views() const;
172
173 inline void update(qreal x, qreal y, qreal w, qreal h)
174 { update(QRectF(x, y, w, h)); }
175 inline void invalidate(qreal x, qreal y, qreal w, qreal h, SceneLayers layers = AllLayers)
176 { invalidate(QRectF(x, y, w, h), layers); }
177
178 QStyle *style() const;
179 void setStyle(QStyle *style);
180
181 QFont font() const;
182 void setFont(const QFont &font);
183
184 QPalette palette() const;
185 void setPalette(const QPalette &palette);
186
187 bool isActive() const;
188 QGraphicsItem *activePanel() const;
189 void setActivePanel(QGraphicsItem *item);
190 QGraphicsWidget *activeWindow() const;
191 void setActiveWindow(QGraphicsWidget *widget);
192
193 bool sendEvent(QGraphicsItem *item, QEvent *event);
194
195 qreal minimumRenderSize() const;
196 void setMinimumRenderSize(qreal minSize);
197
198 bool focusOnTouch() const;
199 void setFocusOnTouch(bool enabled);
200
201public Q_SLOTS:
202 void update(const QRectF &rect = QRectF());
203 void invalidate(const QRectF &rect = QRectF(), SceneLayers layers = AllLayers);
204 void advance();
205 void clearSelection();
206 void clear();
207
208protected:
209 bool event(QEvent *event) override;
210 bool eventFilter(QObject *watched, QEvent *event) override;
211 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
212 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
213 virtual void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
214 virtual void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
215 virtual void dropEvent(QGraphicsSceneDragDropEvent *event);
216 virtual void focusInEvent(QFocusEvent *event);
217 virtual void focusOutEvent(QFocusEvent *event);
218 virtual void helpEvent(QGraphicsSceneHelpEvent *event);
219 virtual void keyPressEvent(QKeyEvent *event);
220 virtual void keyReleaseEvent(QKeyEvent *event);
221 virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
222 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
223 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
224 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
225 virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
226 virtual void inputMethodEvent(QInputMethodEvent *event);
227
228 virtual void drawBackground(QPainter *painter, const QRectF &rect);
229 virtual void drawForeground(QPainter *painter, const QRectF &rect);
230 virtual void drawItems(QPainter *painter, int numItems,
231 QGraphicsItem *items[],
232 const QStyleOptionGraphicsItem options[],
233 QWidget *widget = nullptr);
234
235protected Q_SLOTS:
236 virtual bool focusNextPrevChild(bool next);
237
238Q_SIGNALS:
239 void changed(const QList<QRectF> &region);
240 void sceneRectChanged(const QRectF &rect);
241 void selectionChanged();
242 void focusItemChanged(QGraphicsItem *newFocus, QGraphicsItem *oldFocus, Qt::FocusReason reason);
243
244private:
245 Q_DECLARE_PRIVATE(QGraphicsScene)
246 Q_DISABLE_COPY(QGraphicsScene)
247 Q_PRIVATE_SLOT(d_func(), void _q_emitUpdated())
248 Q_PRIVATE_SLOT(d_func(), void _q_polishItems())
249 Q_PRIVATE_SLOT(d_func(), void _q_processDirtyItems())
250 Q_PRIVATE_SLOT(d_func(), void _q_updateScenePosDescendants())
251 friend class QGraphicsItem;
252 friend class QGraphicsItemPrivate;
253 friend class QGraphicsObject;
254 friend class QGraphicsView;
255 friend class QGraphicsViewPrivate;
256 friend class QGraphicsWidget;
257 friend class QGraphicsWidgetPrivate;
258 friend class QGraphicsEffect;
259 friend class QGraphicsSceneIndex;
260 friend class QGraphicsSceneIndexPrivate;
261 friend class QGraphicsSceneBspTreeIndex;
262 friend class QGraphicsSceneBspTreeIndexPrivate;
263 friend class QGraphicsItemEffectSourcePrivate;
264#ifndef QT_NO_GESTURES
265 friend class QGesture;
266#endif
267};
268
269Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsScene::SceneLayers)
270
271QT_END_NAMESPACE
272
273#endif
The QAbstractGraphicsShapeItem class provides a common base for all path items.
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
QHash< const QGraphicsItem *, QMap< int, QVariant > > data
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,...
QGraphicsPixmapItem::ShapeMode shapeMode
Qt::TransformationMode transformationMode
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 QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
The QGraphicsSimpleTextItem class provides a simple text item that you can add to a QGraphicsScene.
void _q_updateBoundingRect(const QSizeF &)
bool _q_mouseOnEdge(QGraphicsSceneMouseEvent *)
QWidgetTextControl * control
QWidgetTextControl * textControl() const
void sendControlEvent(QEvent *e)
The QGraphicsTextItem class provides a text item that you can add to a QGraphicsScene to display form...
friend class QPainter
\inmodule QtGui
friend class QWidget
Definition qpainter.h:431
\inmodule QtCore\reentrant
Definition qpoint.h:231
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2568
static void formatGraphicsItemHelper(QDebug debug, const QGraphicsItem *item)
bool _qt_movableAncestorIsSelected(const QGraphicsItem *item)
static QRectF setupTextLayout(QTextLayout *layout)
QDebug operator<<(QDebug debug, const QGraphicsObject *item)
static QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen)
QDebug operator<<(QDebug debug, const QGraphicsItem *item)
static bool qt_QGraphicsItem_isObscured(const QGraphicsItem *item, const QGraphicsItem *other, const QRectF &rect)
QT_REQUIRE_CONFIG(graphicsview)
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)