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
graphicsview.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 <QtPrintSupport/qtprintsupportglobal.h>
5
6#include <QtPrintSupport/qabstractprintdialog.h>
7
8#if QT_CONFIG(printdialog)
9
10#include <QStandardItem>
11#include <QtCore/qmimedata.h>
12#include <QtGui/qdrag.h>
13#include <QtOpenGLWidgets/qopenglwidget.h>
14#include <QtPrintSupport/qprintdialog.h>
15#include <QtPrintSupport/qprinter.h>
16#include <QtWidgets/QGraphicsSceneMouseEvent>
17#include <QtWidgets/qdialog.h>
18#include <QtWidgets/qgraphicsview.h>
19
20void graphicsview_snippet_main()
21{
22 //! [0]
23 QGraphicsScene scene;
24 QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
25
26 QGraphicsItem *item = scene.itemAt(50, 50, QTransform());
27 //! [0]
28 Q_UNUSED(rect);
29 Q_UNUSED(item);
30}
31
32void myPopulateScene(QGraphicsScene *)
33{
34 // Intentionally left empty
35}
36
37void snippetThatUsesMyPopulateScene()
38{
39 //! [1]
40 QGraphicsScene scene;
41 myPopulateScene(&scene);
42 QGraphicsView view(&scene);
43 view.show();
44 //! [1]
45}
46
47class CustomItem : public QStandardItem
48{
49public:
50 using QStandardItem::QStandardItem;
51
52 int type() const override { return UserType; }
53 void mousePressEvent(QGraphicsSceneMouseEvent *event);
54 QStandardItem *clone() const override { return new CustomItem; }
55};
56
57
58void printScene()
59{
60 //! [3]
61 QGraphicsScene scene;
62 QPrinter printer;
63 scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
64
65 if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
66 QPainter painter(&printer);
67 painter.setRenderHint(QPainter::Antialiasing);
68 scene.render(&painter);
69 }
70 //! [3]
71}
72
73void pixmapScene()
74{
75 //! [4]
76 QGraphicsScene scene;
77 scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
78
79 QPixmap pixmap;
80 QPainter painter(&pixmap);
81 painter.setRenderHint(QPainter::Antialiasing);
82 scene.render(&painter);
83 painter.end();
84
85 pixmap.save("scene.png");
86 //! [4]
87}
88
89//! [5]
90void CustomItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
91{
92 QMimeData *data = new QMimeData;
93 QDrag *drag = new QDrag(event->widget());
94 drag->setMimeData(data);
95 drag->exec();
96}
97//! [5]
98
99void viewScene()
100{
101 QGraphicsScene scene;
102 //! [6]
103 QGraphicsView view(&scene);
104 QOpenGLWidget *gl = new QOpenGLWidget();
105 QSurfaceFormat format;
106 format.setSamples(4);
107 gl->setFormat(format);
108 view.setViewport(gl);
109 //! [6]
110}
111
112#endif // QT_CONFIG(printdialog)