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
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
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
16 QWidget *widget) override
17 {
18 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
19 }
20};
22
23
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};
38
39
43
44
48
49
52rect.setPos(100, 100);
53
55// returns QPointF(100, 100);
56
58// returns QPointF(0, 0);
60
61
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
72
73
75QRectF CircleItem::boundingRect() const
76{
77 qreal penWidth = 1;
78 return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
79 diameter + penWidth, diameter + penWidth);
80}
82
83
85QPainterPath RoundItem::shape() const
86{
88 path.addEllipse(boundingRect());
89 return path;
90}
92
93
95void RoundRectItem::paint(QPainter *painter,
98{
99 painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
100}
102
103
105static const int ObjectName = 0;
106
109 if (qgraphicsitem_cast<ButtonItem *>(item))
110 item->setData(ObjectName, "Button");
111}
113
114
119
120line->installSceneEventFilter(ellipse);
121// line's events are filtered by ellipse's sceneEventFilter() function.
122
124// ellipse's events are filtered by line's sceneEventFilter() function.
126
127
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}
138
139
141CustomItem::CustomItem()
142{
143 setAcceptDrops(true);
144 ...
145}
146
148{
149 event->setAccepted(event->mimeData()->hasFormat("text/plain"));
150}
152
153
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}
171
172
174void CircleItem::setRadius(qreal newRadius)
175{
176 if (radius != newRadius) {
177 prepareGeometryChange();
178 radius = newRadius;
179 }
180}
182
183
185// Group all selected items together
187
188// Destroy the group, and delete the group item
191
192
195{
196 public:
197 enum { Type = 2 };
198 int type() const override { return Type; }
199 ...
200};
202
204QTransform xform = item->deviceTransform(view->viewportTransform());
205QRect deviceRect = xform.mapRect(rect).toAlignedRect();
206view->viewport()->scroll(dx, dy, deviceRect);
int type() const override
Returns the type of an item as an int.
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:30
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.
QTransform deviceTransform(const QTransform &viewportTransform) const
void setData(int key, const QVariant &value)
Sets this item's custom data for the key key to value.
virtual void dragEnterEvent(QGraphicsSceneDragDropEvent *event)
This event handler, for event event, can be reimplemented to receive drag enter events for this item.
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
This virtual function is called by QGraphicsItem to notify custom items that some part of the item's ...
void installSceneEventFilter(QGraphicsItem *filterItem)
Installs an event filter for this item on filterItem, causing all events for this item to first pass ...
void setPos(const QPointF &pos)
Sets the position of the item to pos, which is in parent coordinates.
QVariant data(int key) const
Returns this item's custom data for the key key as a QVariant.
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
This event handler can be reimplemented in a subclass to process context menu events.
QTransform sceneTransform() const
void setCursor(const QCursor &cursor)
Sets the current cursor shape for the item to 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.
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)
Reparents all items in group to group's parent item, then removes group from the scene,...
QGraphicsEllipseItem * addEllipse(const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
Creates and adds an ellipse item to the scene, and returns the item pointer.
QRectF sceneRect
the scene rectangle; the bounding rectangle of the scene
QGraphicsItemGroup * createItemGroup(const QList< QGraphicsItem * > &items)
Groups all items in items into a new QGraphicsItemGroup, and returns a pointer to the group.
QGraphicsLineItem * addLine(const QLineF &line, const QPen &pen=QPen())
Creates and adds a line item to the scene, and returns the item pointer.
QGraphicsItem * itemAt(const QPointF &pos, const QTransform &deviceTransform) const
\inmodule QtCore\compares equality \compareswith equality QLine \endcompareswith
Definition qline.h:192
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus.
Definition qmenu.h:26
QAction * exec()
Executes this menu synchronously.
Definition qmenu.cpp:2613
void addAction(QAction *action)
Appends the action action to this widget's list of actions.
Definition qwidget.cpp:3117
\inmodule QtGui
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
\inmodule QtCore\reentrant
Definition qpoint.h:217
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:343
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:348
constexpr void setY(qreal y) noexcept
Sets the y coordinate of this point to the given finite y coordinate.
Definition qpoint.h:358
constexpr void setX(qreal x) noexcept
Sets the x coordinate of this point to the given finite x coordinate.
Definition qpoint.h:353
\inmodule QtCore\reentrant
Definition qrect.h:484
\inmodule QtCore\reentrant
Definition qrect.h:30
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
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
QPoint map(const QPoint &p) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QTransform inverted(bool *invertible=nullptr) const
Returns an inverted copy of this matrix.
QRect mapRect(const QRect &) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore
Definition qvariant.h:65
QString toString() const
Returns the variant as a QString if the variant has a userType() including, but not limited to:
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
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...
QOpenGLWidget * widget
[1]
rect
[4]
@ IBeamCursor
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLboolean GLuint group
struct _cl_event * event
GLsizei const GLchar *const * path
GLuint GLenum option
static const QRectF boundingRect(const QPointF *points, int pointCount)
double qreal
Definition qtypes.h:187
QGraphicsScene scene
[0]
QGraphicsRectItem rect
[3]
static const int ObjectName
[10]
QGraphicsItem * item
QGraphicsEllipseItem * ellipse
QPainter painter(this)
[7]
QMenu menu
[5]
QQuickView * view
[0]
Definition moc.h:23