QtBase  v6.3.1
src_gui_graphicsview_qgraphicsitem.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 class SimpleItem : public QGraphicsItem
53 {
54 public:
55  QRectF boundingRect() const override
56  {
57  qreal penWidth = 1;
58  return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
59  20 + penWidth, 20 + penWidth);
60  }
61 
63  QWidget *widget) override
64  {
65  painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
66  }
67 };
69 
70 
72 class CustomItem : public QGraphicsItem
73 {
74 public:
75  enum { Type = UserType + 1 };
76 
77  int type() const override
78  {
79  // Enable the use of qgraphicsitem_cast with this item.
80  return Type;
81  }
82  ...
83 };
85 
86 
90 
91 
95 
96 
99 rect.setPos(100, 100);
100 
102 // returns QPointF(100, 100);
103 
104 rect.sceneTransform().inverted().map(QPointF(100, 100));
105 // returns QPointF(0, 0);
107 
108 
111 rect.setPos(100, 100);
112 
113 rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));
114 // returns the item's (0, 0) point in view's viewport coordinates
115 
116 rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));
117 // returns view's viewport's (100, 100) coordinate in item coordinates
119 
120 
122 // Rotate an item 45 degrees around (0, 0).
123 item->rotate(45);
124 
125 // Rotate an item 45 degrees around (x, y).
126 item->setTransform(QTransform().translate(x, y).rotate(45).translate(-x, -y));
128 
129 
131 // Scale an item by 3x2 from its origin
132 item->scale(3, 2);
133 
134 // Scale an item by 3x2 from (x, y)
135 item->setTransform(QTransform().translate(x, y).scale(3, 2).translate(-x, -y));
137 
138 
140 QRectF CircleItem::boundingRect() const
141 {
142  qreal penWidth = 1;
143  return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
144  diameter + penWidth, diameter + penWidth);
145 }
147 
148 
150 QPainterPath RoundItem::shape() const
151 {
153  path.addEllipse(boundingRect());
154  return path;
155 }
157 
158 
162  QWidget *widget)
163 {
164  painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
165 }
167 
168 
170 static const int ObjectName = 0;
171 
173 if (item->data(ObjectName).toString().isEmpty()) {
174  if (qgraphicsitem_cast<ButtonItem *>(item))
175  item->setData(ObjectName, "Button");
176 }
178 
179 
183 QGraphicsLineItem *line = scene.addLine(QLineF(-10, -10, 20, 20));
184 
186 // line's events are filtered by ellipse's sceneEventFilter() function.
187 
189 // ellipse's events are filtered by line's sceneEventFilter() function.
191 
192 
195 {
196  QMenu menu;
197  QAction *removeAction = menu.addAction("Remove");
198  QAction *markAction = menu.addAction("Mark");
199  QAction *selectedAction = menu.exec(event->screenPos());
200  // ...
201 }
203 
204 
207 {
208  setAcceptDrops(true);
209  ...
210 }
211 
213 {
214  event->setAccepted(event->mimeData()->hasFormat("text/plain"));
215 }
217 
218 
220 QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
221 {
222  if (change == ItemPositionChange && scene()) {
223  // value is the new position.
224  QPointF newPos = value.toPointF();
225  QRectF rect = scene()->sceneRect();
226  if (!rect.contains(newPos)) {
227  // Keep the item inside the scene rect.
228  newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
229  newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
230  return newPos;
231  }
232  }
233  return QGraphicsItem::itemChange(change, value);
234 }
236 
237 
239 void CircleItem::setRadius(qreal newRadius)
240 {
241  if (radius != newRadius) {
242  prepareGeometryChange();
243  radius = newRadius;
244  }
245 }
247 
248 
250 // Group all selected items together
252 
253 // Destroy the group, and delete the group item
256 
257 
260 {
261  public:
262  enum { Type = 2 };
263  int type() const override { return Type; }
264  ...
265 };
267 
269 QTransform xform = item->deviceTransform(view->viewportTransform());
270 QRect deviceRect = xform.mapRect(rect).toAlignedRect();
271 view->viewport()->scroll(dx, dy, deviceRect);
273 
277 
281 
283 setTransform(QTransform().shear(sh, sv), true);
285 
CustomItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent=nullptr)
The QAbstractGraphicsShapeItem class provides a common base for all path items.
The QAction class provides an abstraction for user commands that can be added to different user inter...
Definition: qaction.h:65
The QGraphicsEllipseItem class provides an ellipse item that you can add to a QGraphicsScene.
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.
Definition: qgraphicsitem.h:83
QTransform deviceTransform(const QTransform &viewportTransform) const
void setData(int key, const QVariant &value)
virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
void setTransform(const QTransform &matrix, bool combine=false)
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
qreal scale() const
void installSceneEventFilter(QGraphicsItem *filterItem)
void setPos(const QPointF &pos)
QVariant data(int key) const
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
void setAcceptDrops(bool on)
QTransform sceneTransform() const
void setCursor(const QCursor &cursor)
The QGraphicsLineItem class provides a line item that you can add to a QGraphicsScene.
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
bool contains(const QPointF &point) const override
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 QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void destroyItemGroup(QGraphicsItemGroup *group)
QGraphicsEllipseItem * addEllipse(const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
QRectF sceneRect
the scene rectangle; the bounding rectangle of the scene
QGraphicsItemGroup * createItemGroup(const QList< QGraphicsItem * > &items)
QGraphicsLineItem * addLine(const QLineF &line, const QPen &pen=QPen())
QGraphicsItem * itemAt(const QPointF &pos, const QTransform &deviceTransform) const
The QLineF class provides a two-dimensional vector using floating point precision.
Definition: qline.h:215
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition: qmenu.h:62
QAction * exec()
Definition: qmenu.cpp:2602
void addAction(QAction *action)
Definition: qwidget.cpp:3129
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
Definition: qpainter.cpp:3924
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:65
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:242
constexpr qreal x() const noexcept
Definition: qpoint.h:361
constexpr qreal y() const noexcept
Definition: qpoint.h:366
constexpr void setY(qreal y) noexcept
Definition: qpoint.h:376
constexpr void setX(qreal x) noexcept
Definition: qpoint.h:371
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
bool isEmpty() const
Definition: qstring.h:1216
The QStyleOptionGraphicsItem class is used to describe the parameters needed to draw a QGraphicsItem.
Definition: qstyleoption.h:684
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
static QTransform fromScale(qreal dx, qreal dy)
Definition: qtransform.cpp:503
QPoint map(const QPoint &p) const
static QTransform fromTranslate(qreal dx, qreal dy)
Definition: qtransform.cpp:437
QTransform inverted(bool *invertible=nullptr) const
Definition: qtransform.cpp:339
QRect mapRect(const QRect &) const
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
QString toString() const
Definition: qvariant.cpp:1416
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:133
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
QOpenGLWidget * widget
[1]
QPainter paint
@ IBeamCursor
Definition: qnamespace.h:1181
EGLOutputLayerEXT EGLint EGLAttrib value
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
GLint GLint GLint GLint GLint x
[0]
GLboolean GLuint group
GLfloat angle
GLint y
struct _cl_event * event
Definition: qopenglext.h:2998
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
GLuint GLenum option
Definition: qopenglext.h:5929
item setTransform(QTransform().translate(x, y).rotate(45).translate(-x, -y))
QGraphicsScene scene
[11]
QGraphicsRectItem rect
[3]
QGraphicsLineItem * line
QGraphicsItem * item
item scale(3, 2)
[6]
QTransform xform
[18]
QGraphicsEllipseItem * ellipse
rect rect item rotate(45)
[5]
QPainter painter(this)
[7]
QMenu menu
[5]
QQuickView * view
[0]
Definition: moc.h:48