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
src_gui_graphicsview_qgraphicsitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
6//! [0]
8{
9public:
10 QRectF boundingRect() const override
11 {
12 qreal penWidth = 1;
13 return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
14 20 + penWidth, 20 + penWidth);
15 }
16
17 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
18 QWidget *widget) override
19 {
20 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
21 }
22};
23//! [0]
24
25
26//! [1]
27class Item : public QGraphicsItem
28{
29public:
30 enum { Type = UserType + 1 };
31
32 int type() const override
33 {
34 // Enable the use of qgraphicsitem_cast with this item.
35 return Type;
36 }
37 //...
38};
39//! [1]
40
41void example(QGraphicsItem *item, QGraphicsView *view)
42{
43 {
44 //! [2]
45 item->setCursor(Qt::IBeamCursor);
46 //! [2]
47 }
48
49 {
50 //! [3]
51 item->setCursor(Qt::IBeamCursor);
52 //! [3]
53 }
54
55 {
56 //! [4]
57 QGraphicsRectItem rect;
58 rect.setPos(100, 100);
59
60 rect.sceneTransform().map(QPointF(0, 0));
61 // returns QPointF(100, 100);
62
63 rect.sceneTransform().inverted().map(QPointF(100, 100));
64 // returns QPointF(0, 0);
65 //! [4]
66 }
67
68 {
69 //! [5]
70 QGraphicsRectItem rect;
71 rect.setPos(100, 100);
72
73 rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));
74 // returns the item's (0, 0) point in view's viewport coordinates
75
76 rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));
77 // returns view's viewport's (100, 100) coordinate in item coordinates
78 //! [5]
79 }
80}
81
83{
84 public:
86 QRectF boundingRect() const override;
87 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
88 QWidget *widget) override;
89 QPainterPath shape() const override;
90 void setRadius(qreal newRadius);
91 void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
92 void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
93 QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
94 private:
95 qreal radius;
96 qreal diameter;
97
98 void setAcceptDrops(bool on);
99};
100
105
106//! [8]
108{
109 qreal penWidth = 1;
110 return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
111 diameter + penWidth, diameter + penWidth);
112}
113//! [8]
114
115//! [9]
117{
118 QPainterPath path;
119 path.addEllipse(boundingRect());
120 return path;
121}
122//! [9]
123
124//! [10]
125void RoundRectItem::paint(QPainter *painter,
126 const QStyleOptionGraphicsItem *option,
127 QWidget *widget)
128{
129 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
130}
131//! [10]
132
133void data_snippet(QGraphicsScene &scene, QTransform &transform)
134{
135 //! [11]
136 static const int ObjectName = 0;
137
138 QGraphicsItem *item = scene.itemAt(100, 50, transform);
139 if (item->data(ObjectName).toString().isEmpty()) {
140 if (qgraphicsitem_cast<CustomItem *>(item))
141 item->setData(ObjectName, "Custom");
142 }
143 //! [11]
144}
145
147{
148 //! [12]
149 QGraphicsScene scene;
150 QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
151 QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
152
153 line->installSceneEventFilter(ellipse);
154 // line's events are filtered by ellipse's sceneEventFilter() function.
155
156 ellipse->installSceneEventFilter(line);
157 // ellipse's events are filtered by line's sceneEventFilter() function.
158 //! [12]
159}
160
161//! [13]
162void CustomItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
163{
164 QMenu menu;
165 QAction *removeAction = menu.addAction("Remove");
166 QAction *markAction = menu.addAction("Mark");
167 QAction *selectedAction = menu.exec(event->screenPos());
168 // ...
169}
170//! [13]
171
172
173//! [14]
175{
176 setAcceptDrops(true);
177 //...
178}
179
180void CustomItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
181{
182 event->setAccepted(event->mimeData()->hasFormat("text/plain"));
183}
184//! [14]
185
186
187//! [15]
188QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
189{
190 if (change == ItemPositionChange && scene()) {
191 // value is the new position.
192 QPointF newPos = value.toPointF();
193 QRectF rect = scene()->sceneRect();
194 if (!rect.contains(newPos)) {
195 // Keep the item inside the scene rect.
196 newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
197 newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
198 return newPos;
199 }
200 }
201 return QGraphicsItem::itemChange(change, value);
202}
203//! [15]
204
205//! [16]
206void CircleItem::setRadius(qreal newRadius)
207{
208 if (radius != newRadius) {
209 prepareGeometryChange();
210 radius = newRadius;
211 }
212}
213//! [16]
214
215void snippets( QGraphicsScene *scene, QGraphicsItem *item,
216 QGraphicsView *view, const QRectF &rect, int dx, int dy)
217{
218 //! [17]
219 // Group all selected items together
220 QGraphicsItemGroup *group = scene->createItemGroup(scene->selectedItems());
221
222 // Destroy the group, and delete the group item
223 scene->destroyItemGroup(group);
224 //! [17]
225
226 //! [19]
227 QTransform xform = item->deviceTransform(view->viewportTransform());
228 QRect deviceRect = xform.mapRect(rect).toAlignedRect();
229 view->viewport()->scroll(dx, dy, deviceRect);
230 //! [19]
231}
232
233/* For convenient quoting in the documentation
234//! [18]
235class QGraphicsPathItem : public QAbstractGraphicsShapeItem
236{
237 public:
238 enum { Type = 2 };
239 int type() const override { return Type; }
240 //...
241};
242//! [18]
243*/
void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override
This event handler, for event event, can be reimplemented to receive drag enter events for this item.
QRectF boundingRect() const override
[8]
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
[9]
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
[13]
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
[14]
void setRadius(qreal newRadius)
[15]
QPainterPath shape() const override
[8]
[0]
Definition lalr.h:84
int type() const override
Returns the type of an item as an int.
QRectF boundingRect() const override
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
This function, which is usually called by QGraphicsView, paints the contents of an item in local coor...
void example(QAbstractAnimation *anim1, QAbstractAnimation *anim2)
void data_snippet(QGraphicsScene &scene, QTransform &transform)
[10]
void snippets(QGraphicsScene *scene, QGraphicsItem *item, QGraphicsView *view, const QRectF &rect, int dx, int dy)
[16]