QtBase  v6.3.1
graphicsview.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 #include <QStandardItem>
51 #include <QtCore/qmimedata.h>
52 #include <QtGui/qdrag.h>
53 #include <QtOpenGLWidgets/qopenglwidget.h>
54 #include <QtPrintSupport/qprintdialog.h>
55 #include <QtPrintSupport/qprinter.h>
56 #include <QtWidgets/QGraphicsSceneMouseEvent>
57 #include <QtWidgets/qdialog.h>
58 #include <QtWidgets/qgraphicsview.h>
59 
61 {
64 QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
65 
68 Q_UNUSED(rect);
69 Q_UNUSED(item);
70 }
71 
73 {
74  // Intentionally left empty
75 }
76 
78 {
83 view.show();
85 }
86 
87 class CustomItem : public QStandardItem
88 {
89 public:
91 
92  int type() const override { return UserType; }
94  QStandardItem *clone() const override { return new CustomItem; }
95 };
96 
97 
98 void printScene()
99 {
102 QPrinter printer;
103 scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
104 
105 if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
106  QPainter painter(&printer);
108  scene.render(&painter);
109 }
111 }
112 
114 {
117 scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
118 
123 painter.end();
124 
125 pixmap.save("scene.png");
127 }
128 
131 {
132  QMimeData *data = new QMimeData;
133  QDrag *drag = new QDrag(event->widget());
134  drag->setMimeData(data);
135  drag->exec();
136 }
138 
139 void viewScene()
140 {
144 QOpenGLWidget *gl = new QOpenGLWidget();
146 format.setSamples(4);
147 gl->setFormat(format);
148 view.setViewport(gl);
150 }
CustomItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent=nullptr)
int type() const override
void mousePressEvent(QGraphicsSceneMouseEvent *event)
[5]
QStandardItem * clone() const override
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:66
@ Accepted
Definition: qdialog.h:66
The QDrag class provides support for MIME-based drag and drop data transfer.
Definition: qdrag.h:58
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition: qdrag.cpp:235
void setMimeData(QMimeData *data)
Definition: qdrag.cpp:135
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
Definition: qgraphicsitem.h:83
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
QGraphicsRectItem * addRect(const QRectF &rect, const QPen &pen=QPen(), const QBrush &brush=QBrush())
void render(QPainter *painter, const QRectF &target=QRectF(), const QRectF &source=QRectF(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
QGraphicsItem * itemAt(const QPointF &pos, const QTransform &deviceTransform) const
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
Definition: qgraphicsview.h:60
The QMimeData class provides a container for data that records information about its MIME type.
Definition: qmimedata.h:52
The QOpenGLWidget class is a widget for rendering OpenGL graphics.
Definition: qopenglwidget.h:54
void setFormat(const QSurfaceFormat &format)
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
@ Antialiasing
Definition: qpainter.h:88
bool end()
Definition: qpainter.cpp:1877
void setRenderHint(RenderHint hint, bool on=true)
Definition: qpainter.cpp:6845
The QPen class defines how a QPainter should draw lines and outlines of shapes.
Definition: qpen.h:61
The QPixmap class is an off-screen image representation that can be used as a paint device.
Definition: qpixmap.h:63
The QPrintDialog class provides a dialog for specifying the printer's configuration.
Definition: qprintdialog.h:56
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:64
The QRectF class defines a finite rectangle in the plane using floating point precision.
Definition: qrect.h:511
The QStandardItem class provides an item for use with the QStandardItemModel class.
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
msgBox exec()
rect
[4]
@ black
Definition: qnamespace.h:61
@ green
Definition: qnamespace.h:67
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei GLsizei GLenum format
struct _cl_event * event
Definition: qopenglext.h:2998
Q_UNUSED(salary)
[21]
void printScene()
void viewScene()
[5]
void snippetThatUsesMyPopulateScene()
void pixmapScene()
void graphicsview_snippet_main()
void myPopulateScene(QGraphicsScene *)
QGraphicsScene scene
[0]
QGraphicsItem * item
widget render & pixmap
QPainter painter(this)
[7]
QQuickView * view
[0]