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//! [0]
6{
7public:
8 QRectF boundingRect() const override
9 {
10 qreal penWidth = 1;
11 return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
12 20 + penWidth, 20 + penWidth);
13 }
14
15 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
16 QWidget *widget) override
17 {
18 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
19 }
20};
21//! [0]
22
23
24//! [1]
26{
27public:
28 enum { Type = UserType + 1 };
29
30 int type() const override
31 {
32 // Enable the use of qgraphicsitem_cast with this item.
33 return Type;
34 }
35 ...
36};
37//! [1]
38
39
40//! [2]
41item->setCursor(Qt::IBeamCursor);
42//! [2]
43
44
45//! [3]
46item->setCursor(Qt::IBeamCursor);
47//! [3]
48
49
50//! [4]
52rect.setPos(100, 100);
53
54rect.sceneTransform().map(QPointF(0, 0));
55// returns QPointF(100, 100);
56
57rect.sceneTransform().inverted().map(QPointF(100, 100));
58// returns QPointF(0, 0);
59//! [4]
60
61
62//! [5]
64rect.setPos(100, 100);
65
66rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));
67// returns the item's (0, 0) point in view's viewport coordinates
68
69rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));
70// returns view's viewport's (100, 100) coordinate in item coordinates
71//! [5]
72
73
74//! [8]
75QRectF CircleItem::boundingRect() const
76{
77 qreal penWidth = 1;
78 return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
79 diameter + penWidth, diameter + penWidth);
80}
81//! [8]
82
83
84//! [9]
85QPainterPath RoundItem::shape() const
86{
87 QPainterPath path;
88 path.addEllipse(boundingRect());
89 return path;
90}
91//! [9]
92
93
94//! [10]
95void RoundRectItem::paint(QPainter *painter,
96 const QStyleOptionGraphicsItem *option,
97 QWidget *widget)
98{
99 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
100}
101//! [10]
102
103
104//! [11]
105static const int ObjectName = 0;
106
107QGraphicsItem *item = scene.itemAt(100, 50);
108if (item->data(ObjectName).toString().isEmpty()) {
109 if (qgraphicsitem_cast<ButtonItem *>(item))
110 item->setData(ObjectName, "Button");
111}
112//! [11]
113
114
115//! [12]
117QGraphicsEllipseItem *ellipse = scene.addEllipse(QRectF(-10, -10, 20, 20));
118QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
119
120line->installSceneEventFilter(ellipse);
121// line's events are filtered by ellipse's sceneEventFilter() function.
122
123ellipse->installSceneEventFilter(line);
124// ellipse's events are filtered by line's sceneEventFilter() function.
125//! [12]
126
127
128//! [13]
129void CustomItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
130{
131 QMenu menu;
132 QAction *removeAction = menu.addAction("Remove");
133 QAction *markAction = menu.addAction("Mark");
134 QAction *selectedAction = menu.exec(event->screenPos());
135 // ...
136}
137//! [13]
138
139
140//! [14]
141CustomItem::CustomItem()
142{
143 setAcceptDrops(true);
144 ...
145}
146
147void CustomItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
148{
149 event->setAccepted(event->mimeData()->hasFormat("text/plain"));
150}
151//! [14]
152
153
154//! [15]
155QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
156{
157 if (change == ItemPositionChange && scene()) {
158 // value is the new position.
159 QPointF newPos = value.toPointF();
160 QRectF rect = scene()->sceneRect();
161 if (!rect.contains(newPos)) {
162 // Keep the item inside the scene rect.
163 newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
164 newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
165 return newPos;
166 }
167 }
168 return QGraphicsItem::itemChange(change, value);
169}
170//! [15]
171
172
173//! [16]
174void CircleItem::setRadius(qreal newRadius)
175{
176 if (radius != newRadius) {
177 prepareGeometryChange();
178 radius = newRadius;
179 }
180}
181//! [16]
182
183
184//! [17]
185// Group all selected items together
186QGraphicsItemGroup *group = scene->createItemGroup(scene->selecteditems());
187
188// Destroy the group, and delete the group item
189scene->destroyItemGroup(group);
190//! [17]
191
192
193//! [18]
195{
196 public:
197 enum { Type = 2 };
198 int type() const override { return Type; }
199 ...
200};
201//! [18]
202
203//! [19]
204QTransform xform = item->deviceTransform(view->viewportTransform());
205QRect deviceRect = xform.mapRect(rect).toAlignedRect();
206view->viewport()->scroll(dx, dy, deviceRect);
207//! [19]
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...
rect
[4]
QParallelAnimationGroup * group
[0]
QGraphicsScene scene
[0]
QGraphicsEllipseItem * ellipse
static const int ObjectName
[10]
QGraphicsItem * item