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
qgraphicsview.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 QGRAPHICSVIEW_H
6#define QGRAPHICSVIEW_H
7
8#include <QtWidgets/qtwidgetsglobal.h>
9#include <QtCore/qmetatype.h>
10#include <QtGui/qpainter.h>
11#include <QtWidgets/qscrollarea.h>
12#include <QtWidgets/qgraphicsscene.h>
13
15
16QT_BEGIN_NAMESPACE
17
18class QGraphicsItem;
19class QPainterPath;
20class QPolygonF;
21class QStyleOptionGraphicsItem;
22
23class QGraphicsViewPrivate;
24class Q_WIDGETS_EXPORT QGraphicsView : public QAbstractScrollArea
25{
26 Q_OBJECT
27 Q_PROPERTY(QBrush backgroundBrush READ backgroundBrush WRITE setBackgroundBrush)
28 Q_PROPERTY(QBrush foregroundBrush READ foregroundBrush WRITE setForegroundBrush)
29 Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive)
30 Q_PROPERTY(QRectF sceneRect READ sceneRect WRITE setSceneRect)
31 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
32 Q_PROPERTY(QPainter::RenderHints renderHints READ renderHints WRITE setRenderHints)
33 Q_PROPERTY(DragMode dragMode READ dragMode WRITE setDragMode)
34 Q_PROPERTY(CacheMode cacheMode READ cacheMode WRITE setCacheMode)
35 Q_PROPERTY(ViewportAnchor transformationAnchor READ transformationAnchor
36 WRITE setTransformationAnchor)
37 Q_PROPERTY(ViewportAnchor resizeAnchor READ resizeAnchor WRITE setResizeAnchor)
38 Q_PROPERTY(ViewportUpdateMode viewportUpdateMode READ viewportUpdateMode
39 WRITE setViewportUpdateMode)
40#if QT_CONFIG(rubberband)
41 Q_PROPERTY(Qt::ItemSelectionMode rubberBandSelectionMode READ rubberBandSelectionMode
42 WRITE setRubberBandSelectionMode)
43#endif
44 Q_PROPERTY(OptimizationFlags optimizationFlags READ optimizationFlags
45 WRITE setOptimizationFlags)
47public:
48 enum ViewportAnchor {
49 NoAnchor,
50 AnchorViewCenter,
51 AnchorUnderMouse
52 };
53 Q_ENUM(ViewportAnchor)
55 enum CacheModeFlag {
56 CacheNone = 0x0,
57 CacheBackground = 0x1
58 };
59 Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
60 Q_FLAG(CacheMode)
62 enum DragMode {
63 NoDrag,
64 ScrollHandDrag,
65 RubberBandDrag
66 };
67 Q_ENUM(DragMode)
69 enum ViewportUpdateMode {
70 FullViewportUpdate,
71 MinimalViewportUpdate,
72 SmartViewportUpdate,
73 NoViewportUpdate,
74 BoundingRectViewportUpdate
75 };
76 Q_ENUM(ViewportUpdateMode)
78 enum OptimizationFlag {
79 DontSavePainterState = 0x1,
80 DontAdjustForAntialiasing = 0x2,
81 IndirectPainting = 0x4
82 };
83 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
84 Q_FLAG(OptimizationFlags)
85
86 QGraphicsView(QWidget *parent = nullptr);
87 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
88 ~QGraphicsView();
89
90 QSize sizeHint() const override;
91
92 QPainter::RenderHints renderHints() const;
93 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
94 void setRenderHints(QPainter::RenderHints hints);
95
96 Qt::Alignment alignment() const;
97 void setAlignment(Qt::Alignment alignment);
98
99 ViewportAnchor transformationAnchor() const;
100 void setTransformationAnchor(ViewportAnchor anchor);
101
102 ViewportAnchor resizeAnchor() const;
103 void setResizeAnchor(ViewportAnchor anchor);
104
105 ViewportUpdateMode viewportUpdateMode() const;
106 void setViewportUpdateMode(ViewportUpdateMode mode);
107
108 OptimizationFlags optimizationFlags() const;
109 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
110 void setOptimizationFlags(OptimizationFlags flags);
111
112 DragMode dragMode() const;
113 void setDragMode(DragMode mode);
114
115#if QT_CONFIG(rubberband)
116 Qt::ItemSelectionMode rubberBandSelectionMode() const;
117 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
118 QRect rubberBandRect() const;
119#endif
120
121 CacheMode cacheMode() const;
122 void setCacheMode(CacheMode mode);
123 void resetCachedContent();
124
125 bool isInteractive() const;
126 void setInteractive(bool allowed);
127
128 QGraphicsScene *scene() const;
129 void setScene(QGraphicsScene *scene);
130
131 QRectF sceneRect() const;
132 void setSceneRect(const QRectF &rect);
133 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
134
135 QTransform transform() const;
136 QTransform viewportTransform() const;
137 bool isTransformed() const;
138 void setTransform(const QTransform &matrix, bool combine = false);
139 void resetTransform();
140 void rotate(qreal angle);
141 void scale(qreal sx, qreal sy);
142 void shear(qreal sh, qreal sv);
143 void translate(qreal dx, qreal dy);
144
145 void centerOn(const QPointF &pos);
146 inline void centerOn(qreal x, qreal y);
147 void centerOn(const QGraphicsItem *item);
148 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
149 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
150 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
151 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
152 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
153 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
154 void fitInView(const QGraphicsItem *item,
155 Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
156
157 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
158 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
159
160 QList<QGraphicsItem *> items() const;
161 QList<QGraphicsItem *> items(const QPoint &pos) const;
162 inline QList<QGraphicsItem *> items(int x, int y) const;
163 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
164 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
165 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
166 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
167 QGraphicsItem *itemAt(const QPoint &pos) const;
168 inline QGraphicsItem *itemAt(int x, int y) const;
169
170 QPointF mapToScene(const QPoint &point) const;
171 QPolygonF mapToScene(const QRect &rect) const;
172 QPolygonF mapToScene(const QPolygon &polygon) const;
173 QPainterPath mapToScene(const QPainterPath &path) const;
174 QPoint mapFromScene(const QPointF &point) const;
175 QPolygon mapFromScene(const QRectF &rect) const;
176 QPolygon mapFromScene(const QPolygonF &polygon) const;
177 QPainterPath mapFromScene(const QPainterPath &path) const;
178 inline QPointF mapToScene(int x, int y) const;
179 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
180 inline QPoint mapFromScene(qreal x, qreal y) const;
181 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
182
183 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
184
185 QBrush backgroundBrush() const;
186 void setBackgroundBrush(const QBrush &brush);
187
188 QBrush foregroundBrush() const;
189 void setForegroundBrush(const QBrush &brush);
190
191public Q_SLOTS:
192 void updateScene(const QList<QRectF> &rects);
193 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
194 void updateSceneRect(const QRectF &rect);
195
196#if QT_CONFIG(rubberband)
197Q_SIGNALS:
198 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
199#endif
200
201protected Q_SLOTS:
202 void setupViewport(QWidget *widget) override;
203
204protected:
205 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
206 bool event(QEvent *event) override;
207 bool viewportEvent(QEvent *event) override;
208
209#ifndef QT_NO_CONTEXTMENU
210 void contextMenuEvent(QContextMenuEvent *event) override;
211#endif
212#if QT_CONFIG(draganddrop)
213 void dragEnterEvent(QDragEnterEvent *event) override;
214 void dragLeaveEvent(QDragLeaveEvent *event) override;
215 void dragMoveEvent(QDragMoveEvent *event) override;
216 void dropEvent(QDropEvent *event) override;
217#endif
218 void focusInEvent(QFocusEvent *event) override;
219 bool focusNextPrevChild(bool next) override;
220 void focusOutEvent(QFocusEvent *event) override;
221 void keyPressEvent(QKeyEvent *event) override;
222 void keyReleaseEvent(QKeyEvent *event) override;
223 void mouseDoubleClickEvent(QMouseEvent *event) override;
224 void mousePressEvent(QMouseEvent *event) override;
225 void mouseMoveEvent(QMouseEvent *event) override;
226 void mouseReleaseEvent(QMouseEvent *event) override;
227#if QT_CONFIG(wheelevent)
228 void wheelEvent(QWheelEvent *event) override;
229#endif
230 void paintEvent(QPaintEvent *event) override;
231 void resizeEvent(QResizeEvent *event) override;
232 void scrollContentsBy(int dx, int dy) override;
233 void showEvent(QShowEvent *event) override;
234 void inputMethodEvent(QInputMethodEvent *event) override;
235
236 virtual void drawBackground(QPainter *painter, const QRectF &rect);
237 virtual void drawForeground(QPainter *painter, const QRectF &rect);
238 virtual void drawItems(QPainter *painter, int numItems,
239 QGraphicsItem *items[],
240 const QStyleOptionGraphicsItem options[]);
241
242private:
243 Q_DECLARE_PRIVATE(QGraphicsView)
244 Q_DISABLE_COPY(QGraphicsView)
245#ifndef QT_NO_CURSOR
246 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
247 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
248#endif
249 friend class QGraphicsSceneWidget;
250 friend class QGraphicsScene;
251 friend class QGraphicsScenePrivate;
252 friend class QGraphicsItemPrivate;
253};
254
255Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
256Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
257
258inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
259{ setSceneRect(QRectF(ax, ay, aw, ah)); }
260inline void QGraphicsView::centerOn(qreal ax, qreal ay)
261{ centerOn(QPointF(ax, ay)); }
262inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
263{ ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
264inline void QGraphicsView::fitInView(qreal ax, qreal ay, qreal w, qreal h, Qt::AspectRatioMode mode)
265{ fitInView(QRectF(ax, ay, w, h), mode); }
266inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
267{ return items(QPoint(ax, ay)); }
268inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
269{ return items(QRect(ax, ay, w, h), mode); }
270inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
271{ return itemAt(QPoint(ax, ay)); }
272inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
273{ return mapToScene(QPoint(ax, ay)); }
274inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
275{ return mapToScene(QRect(ax, ay, w, h)); }
276inline QPoint QGraphicsView::mapFromScene(qreal ax, qreal ay) const
277{ return mapFromScene(QPointF(ax, ay)); }
278inline QPolygon QGraphicsView::mapFromScene(qreal ax, qreal ay, qreal w, qreal h) const
279{ return mapFromScene(QRectF(ax, ay, w, h)); }
280
281QT_END_NAMESPACE
282
283#endif // QGRAPHICSVIEW_H
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,...
The QGraphicsPathItem class provides a path item that you can add to a QGraphicsScene.
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 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 QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
The QGraphicsSceneMoveEvent class provides events for widget moving in the graphics view framework.
\inmodule QtWidgets
The QGraphicsSceneResizeEvent class provides events for widget resizing in the graphics view framewor...
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.
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...
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
friend class QPainter
\inmodule QtGui
friend class QWidget
Definition qpainter.h:431
\inmodule QtCore\reentrant
Definition qpoint.h:232
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
Combined button and popup list for selecting options.
QT_REQUIRE_CONFIG(animation)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
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)
static QRectF adjustedItemBoundingRect(const QGraphicsItem *item)
static QRectF adjustedItemEffectiveBoundingRect(const QGraphicsItem *item)
static void _q_adjustRect(QRectF *rect)
QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)