Qt 6.x
The Qt SDK
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#include <QStandardItem>
4#include <QtCore/qmimedata.h>
5#include <QtGui/qdrag.h>
6#include <QtOpenGLWidgets/qopenglwidget.h>
7#include <QtPrintSupport/qprintdialog.h>
8#include <QtPrintSupport/qprinter.h>
9#include <QtWidgets/QGraphicsSceneMouseEvent>
10#include <QtWidgets/qdialog.h>
11#include <QtWidgets/qgraphicsview.h>
12
14{
17QGraphicsRectItem *rect = scene.addRect(QRectF(0, 0, 100, 100));
18
23}
24
26{
27 // Intentionally left empty
28}
29
31{
36view.show();
38}
39
40class CustomItem : public QStandardItem
41{
42public:
44
45 int type() const override { return UserType; }
47 QStandardItem *clone() const override { return new CustomItem; }
48};
49
50
52{
55QPrinter printer;
56scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
57
58if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
59 QPainter painter(&printer);
62}
64}
65
67{
70scene.addRect(QRectF(0, 0, 100, 200), QPen(Qt::black), QBrush(Qt::green));
71
76painter.end();
77
78pixmap.save("scene.png");
80}
81
84{
86 QDrag *drag = new QDrag(event->widget());
87 drag->setMimeData(data);
88 drag->exec();
89}
91
93{
99format.setSamples(4);
100gl->setFormat(format);
101view.setViewport(gl);
103}
int type() const override
Returns the type of an item as an int.
void mousePressEvent(QGraphicsSceneMouseEvent *event)
[5]
QStandardItem * clone() const override
Returns a copy of this item.
\inmodule QtGui
Definition qbrush.h:30
@ Accepted
Definition qdialog.h:30
\inmodule QtGui
Definition qdrag.h:22
Qt::DropAction exec(Qt::DropActions supportedActions=Qt::MoveAction)
Definition qdrag.cpp:199
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition qdrag.cpp:99
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsRectItem class provides a rectangle item that you can add to a QGraphicsScene.
The QGraphicsSceneMouseEvent class provides mouse events in the graphics view framework.
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())
Creates and adds a rectangle item to the scene, and returns the item pointer.
void render(QPainter *painter, const QRectF &target=QRectF(), const QRectF &source=QRectF(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
Renders the source rect from scene into target, using painter.
QGraphicsItem * itemAt(const QPointF &pos, const QTransform &deviceTransform) const
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
\inmodule QtCore
Definition qmimedata.h:16
\inmodule QtOpenGLWidgets
void setFormat(const QSurfaceFormat &format)
Sets the requested surface format.
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
@ Antialiasing
Definition qpainter.h:52
bool end()
Ends painting.
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint.
\inmodule QtGui
Definition qpen.h:25
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
The QPrintDialog class provides a dialog for specifying the printer's configuration.
\reentrant
Definition qprinter.h:28
\inmodule QtCore\reentrant
Definition qrect.h:483
The QStandardItem class provides an item for use with the QStandardItemModel class.
QStandardItem()
Constructs an item.
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
void show()
Shows the window.
Definition qwindow.cpp:2181
rect
[4]
void printScene()
void viewScene()
[5]
void snippetThatUsesMyPopulateScene()
void pixmapScene()
void graphicsview_snippet_main()
void myPopulateScene(QGraphicsScene *)
@ black
Definition qnamespace.h:29
@ green
Definition qnamespace.h:35
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei GLsizei GLenum format
struct _cl_event * event
#define Q_UNUSED(x)
dialog exec()
QGraphicsScene scene
[0]
QGraphicsItem * item
widget render & pixmap
QPainter painter(this)
[7]
QQuickView * view
[0]