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
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
4#ifndef QGRAPHICSVIEW_H
5#define QGRAPHICSVIEW_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qmetatype.h>
9#include <QtGui/qpainter.h>
10#include <QtWidgets/qscrollarea.h>
11#include <QtWidgets/qgraphicsscene.h>
12
13QT_REQUIRE_CONFIG(graphicsview);
14
16
17class QGraphicsItem;
18class QPainterPath;
19class QPolygonF;
21
23class Q_WIDGETS_EXPORT QGraphicsView : public QAbstractScrollArea
24{
26 Q_FLAGS(QPainter::RenderHints CacheMode OptimizationFlags)
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)
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)
46
47public:
51 AnchorUnderMouse
52 };
53 Q_ENUM(ViewportAnchor)
54
56 CacheNone = 0x0,
57 CacheBackground = 0x1
58 };
59 Q_DECLARE_FLAGS(CacheMode, CacheModeFlag)
60
61 enum DragMode {
64 RubberBandDrag
65 };
66 Q_ENUM(DragMode)
67
75 Q_ENUM(ViewportUpdateMode)
76
78 DontSavePainterState = 0x1,
79 DontAdjustForAntialiasing = 0x2,
80 IndirectPainting = 0x4
81 };
82 Q_DECLARE_FLAGS(OptimizationFlags, OptimizationFlag)
83
84 QGraphicsView(QWidget *parent = nullptr);
85 QGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
87
88 QSize sizeHint() const override;
89
90 QPainter::RenderHints renderHints() const;
91 void setRenderHint(QPainter::RenderHint hint, bool enabled = true);
92 void setRenderHints(QPainter::RenderHints hints);
93
94 Qt::Alignment alignment() const;
95 void setAlignment(Qt::Alignment alignment);
96
97 ViewportAnchor transformationAnchor() const;
98 void setTransformationAnchor(ViewportAnchor anchor);
99
100 ViewportAnchor resizeAnchor() const;
101 void setResizeAnchor(ViewportAnchor anchor);
102
103 ViewportUpdateMode viewportUpdateMode() const;
104 void setViewportUpdateMode(ViewportUpdateMode mode);
105
106 OptimizationFlags optimizationFlags() const;
107 void setOptimizationFlag(OptimizationFlag flag, bool enabled = true);
108 void setOptimizationFlags(OptimizationFlags flags);
109
110 DragMode dragMode() const;
111 void setDragMode(DragMode mode);
112
113#if QT_CONFIG(rubberband)
114 Qt::ItemSelectionMode rubberBandSelectionMode() const;
115 void setRubberBandSelectionMode(Qt::ItemSelectionMode mode);
116 QRect rubberBandRect() const;
117#endif
118
119 CacheMode cacheMode() const;
120 void setCacheMode(CacheMode mode);
121 void resetCachedContent();
122
123 bool isInteractive() const;
124 void setInteractive(bool allowed);
125
126 QGraphicsScene *scene() const;
127 void setScene(QGraphicsScene *scene);
128
129 QRectF sceneRect() const;
130 void setSceneRect(const QRectF &rect);
131 inline void setSceneRect(qreal x, qreal y, qreal w, qreal h);
132
133 QTransform transform() const;
134 QTransform viewportTransform() const;
135 bool isTransformed() const;
136 void setTransform(const QTransform &matrix, bool combine = false);
137 void resetTransform();
138 void rotate(qreal angle);
139 void scale(qreal sx, qreal sy);
140 void shear(qreal sh, qreal sv);
141 void translate(qreal dx, qreal dy);
142
143 void centerOn(const QPointF &pos);
144 inline void centerOn(qreal x, qreal y);
145 void centerOn(const QGraphicsItem *item);
146 void ensureVisible(const QRectF &rect, int xmargin = 50, int ymargin = 50);
147 inline void ensureVisible(qreal x, qreal y, qreal w, qreal h, int xmargin = 50, int ymargin = 50);
148 void ensureVisible(const QGraphicsItem *item, int xmargin = 50, int ymargin = 50);
149 void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode = Qt::IgnoreAspectRatio);
150 inline void fitInView(qreal x, qreal y, qreal w, qreal h,
152 void fitInView(const QGraphicsItem *item,
154
155 void render(QPainter *painter, const QRectF &target = QRectF(), const QRect &source = QRect(),
156 Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
157
158 QList<QGraphicsItem *> items() const;
159 QList<QGraphicsItem *> items(const QPoint &pos) const;
160 inline QList<QGraphicsItem *> items(int x, int y) const;
161 QList<QGraphicsItem *> items(const QRect &rect, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
162 inline QList<QGraphicsItem *> items(int x, int y, int w, int h, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
163 QList<QGraphicsItem *> items(const QPolygon &polygon, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
164 QList<QGraphicsItem *> items(const QPainterPath &path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape) const;
165 QGraphicsItem *itemAt(const QPoint &pos) const;
166 inline QGraphicsItem *itemAt(int x, int y) const;
167
168 QPointF mapToScene(const QPoint &point) const;
169 QPolygonF mapToScene(const QRect &rect) const;
170 QPolygonF mapToScene(const QPolygon &polygon) const;
171 QPainterPath mapToScene(const QPainterPath &path) const;
172 QPoint mapFromScene(const QPointF &point) const;
173 QPolygon mapFromScene(const QRectF &rect) const;
174 QPolygon mapFromScene(const QPolygonF &polygon) const;
175 QPainterPath mapFromScene(const QPainterPath &path) const;
176 inline QPointF mapToScene(int x, int y) const;
177 inline QPolygonF mapToScene(int x, int y, int w, int h) const;
178 inline QPoint mapFromScene(qreal x, qreal y) const;
179 inline QPolygon mapFromScene(qreal x, qreal y, qreal w, qreal h) const;
180
181 QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
182
183 QBrush backgroundBrush() const;
184 void setBackgroundBrush(const QBrush &brush);
185
186 QBrush foregroundBrush() const;
187 void setForegroundBrush(const QBrush &brush);
188
189public Q_SLOTS:
190 void updateScene(const QList<QRectF> &rects);
191 void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
192 void updateSceneRect(const QRectF &rect);
193
194#if QT_CONFIG(rubberband)
196 void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
197#endif
198
199protected Q_SLOTS:
200 void setupViewport(QWidget *widget) override;
201
202protected:
203 QGraphicsView(QGraphicsViewPrivate &, QWidget *parent = nullptr);
204 bool event(QEvent *event) override;
205 bool viewportEvent(QEvent *event) override;
206
207#ifndef QT_NO_CONTEXTMENU
208 void contextMenuEvent(QContextMenuEvent *event) override;
209#endif
210#if QT_CONFIG(draganddrop)
211 void dragEnterEvent(QDragEnterEvent *event) override;
212 void dragLeaveEvent(QDragLeaveEvent *event) override;
213 void dragMoveEvent(QDragMoveEvent *event) override;
214 void dropEvent(QDropEvent *event) override;
215#endif
216 void focusInEvent(QFocusEvent *event) override;
217 bool focusNextPrevChild(bool next) override;
218 void focusOutEvent(QFocusEvent *event) override;
219 void keyPressEvent(QKeyEvent *event) override;
220 void keyReleaseEvent(QKeyEvent *event) override;
221 void mouseDoubleClickEvent(QMouseEvent *event) override;
222 void mousePressEvent(QMouseEvent *event) override;
223 void mouseMoveEvent(QMouseEvent *event) override;
224 void mouseReleaseEvent(QMouseEvent *event) override;
225#if QT_CONFIG(wheelevent)
226 void wheelEvent(QWheelEvent *event) override;
227#endif
228 void paintEvent(QPaintEvent *event) override;
229 void resizeEvent(QResizeEvent *event) override;
230 void scrollContentsBy(int dx, int dy) override;
231 void showEvent(QShowEvent *event) override;
232 void inputMethodEvent(QInputMethodEvent *event) override;
233
234 virtual void drawBackground(QPainter *painter, const QRectF &rect);
235 virtual void drawForeground(QPainter *painter, const QRectF &rect);
236 virtual void drawItems(QPainter *painter, int numItems,
238 const QStyleOptionGraphicsItem options[]);
239
240private:
241 Q_DECLARE_PRIVATE(QGraphicsView)
242 Q_DISABLE_COPY(QGraphicsView)
243#ifndef QT_NO_CURSOR
244 Q_PRIVATE_SLOT(d_func(), void _q_setViewportCursor(const QCursor &))
245 Q_PRIVATE_SLOT(d_func(), void _q_unsetViewportCursor())
246#endif
247 friend class QGraphicsSceneWidget;
248 friend class QGraphicsScene;
251};
252
253Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::CacheMode)
254Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsView::OptimizationFlags)
255
256inline void QGraphicsView::setSceneRect(qreal ax, qreal ay, qreal aw, qreal ah)
257{ setSceneRect(QRectF(ax, ay, aw, ah)); }
259{ centerOn(QPointF(ax, ay)); }
260inline void QGraphicsView::ensureVisible(qreal ax, qreal ay, qreal aw, qreal ah, int xmargin, int ymargin)
261{ ensureVisible(QRectF(ax, ay, aw, ah), xmargin, ymargin); }
264inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay) const
265{ return items(QPoint(ax, ay)); }
266inline QList<QGraphicsItem *> QGraphicsView::items(int ax, int ay, int w, int h, Qt::ItemSelectionMode mode) const
267{ return items(QRect(ax, ay, w, h), mode); }
268inline QGraphicsItem *QGraphicsView::itemAt(int ax, int ay) const
269{ return itemAt(QPoint(ax, ay)); }
270inline QPointF QGraphicsView::mapToScene(int ax, int ay) const
271{ return mapToScene(QPoint(ax, ay)); }
272inline QPolygonF QGraphicsView::mapToScene(int ax, int ay, int w, int h) const
273{ return mapToScene(QRect(ax, ay, w, h)); }
275{ return mapFromScene(QPointF(ax, ay)); }
277{ return mapFromScene(QRectF(ax, ay, w, h)); }
278
280
281#endif // QGRAPHICSVIEW_H
\inmodule QtGui
Definition qbrush.h:30
The QContextMenuEvent class contains parameters that describe a context menu event.
Definition qevent.h:594
The QCursor class provides a mouse cursor with an arbitrary shape.
Definition qcursor.h:45
\inmodule QtCore
Definition qcoreevent.h:45
The QFocusEvent class contains event parameters for widget focus events.
Definition qevent.h:470
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.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
QPointF mapToScene(const QPoint &point) const
Returns the viewport coordinate point mapped to scene coordinates.
QList< QGraphicsItem * > items() const
Returns a list of all the items in the associated scene, in descending stacking order (i....
void centerOn(const QPointF &pos)
Scrolls the contents of the viewport to ensure that the scene coordinate pos, is centered in the view...
void ensureVisible(const QRectF &rect, int xmargin=50, int ymargin=50)
Scrolls the contents of the viewport so that the scene rectangle rect is visible, with margins specif...
ViewportAnchor
This enums describe the possible anchors that QGraphicsView can use when the user resizes the view or...
CacheModeFlag
This enum describes the flags that you can set for a QGraphicsView's cache mode.
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode=Qt::IgnoreAspectRatio)
Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits insid...
DragMode
This enum describes the default action for the view when pressing and dragging the mouse over the vie...
QGraphicsItem * itemAt(const QPoint &pos) const
Returns the item at position pos, which is in viewport coordinates.
QPoint mapFromScene(const QPointF &point) const
Returns the scene coordinate point to viewport coordinates.
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 QtGui
Definition qevent.h:196
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
RenderHint
Renderhints are used to specify flags to QPainter that may or may not be respected by any given engin...
Definition qpainter.h:51
\inmodule QtCore\reentrant
Definition qpoint.h:217
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
The QPolygon class provides a list of points using integer precision.
Definition qpolygon.h:23
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
The QShowEvent class provides an event that is sent when a widget is shown.
Definition qevent.h:578
\inmodule QtCore
Definition qsize.h:25
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
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]
rect
[4]
uint alignment
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
Definition qcompare.h:63
InputMethodQuery
AspectRatioMode
@ KeepAspectRatio
@ IgnoreAspectRatio
ItemSelectionMode
@ IntersectsItemShape
Definition brush.cpp:5
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]
GLenum mode
GLfloat GLfloat GLfloat w
[0]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLfloat angle
GLenum target
GLbitfield flags
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLsizei GLchar * source
struct _cl_event * event
GLuint GLenum GLenum transform
GLenum query
GLuint GLenum matrix
GLsizei const GLchar *const * path
GLenum GLenum GLenum GLenum GLenum scale
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#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_FLAGS(x)
#define Q_SLOTS
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
double qreal
Definition qtypes.h:187
static bool translate(xcb_connection_t *connection, xcb_window_t child, xcb_window_t parent, int *x, int *y)
label setAlignment(Qt::AlignLeft|Qt::AlignTop)
[0]
QGraphicsScene scene
[0]
QGraphicsItem * item
view setBackgroundBrush(QImage(":/images/backgroundtile.png"))
view setCacheMode(QGraphicsView::CacheBackground)
view setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform)
QList< QTreeWidgetItem * > items
QPainter painter(this)
[7]
myWidget render(this)