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_qgraphicsview.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
6scene.addText("Hello, world!");
7
9view.show();
11
12
15scene.addRect(QRectF(-10, -10, 20, 20));
16
19view.show();
21
22
25view.setBackgroundBrush(QImage(":/images/backgroundtile.png"));
28
29
32scene.addItem(...
33...
34
36view.show();
37...
38
39QPrinter printer(QPrinter::HighResolution);
40printer.setPageSize(QPrinter::A4);
41QPainter painter(&printer);
42
43// print, fitting the viewport contents into a full page
45
46// print the upper half of the viewport into the lower.
47// half of the page.
48QRect viewport = view.viewport()->rect();
50 QRectF(0, printer.height() / 2,
51 printer.width(), printer.height() / 2),
52 viewport.adjusted(0, 0, 0, -viewport.height() / 2));
53
55
56
58void CustomView::mousePressEvent(QMouseEvent *event)
59{
60 qDebug() << "There are" << items(event->pos()).size()
61 << "items at position" << mapToScene(event->pos());
62}
64
65
67void CustomView::mousePressEvent(QMouseEvent *event)
68{
69 if (QGraphicsItem *item = itemAt(event->pos())) {
70 qDebug() << "You clicked on item" << item;
71 } else {
72 qDebug("You didn't click on an item.");
73 }
74}
76
77
80scene.addText("GraphicsView rotated clockwise");
81
83view.rotate(90); // the text is rendered with a 90 degree clockwise rotation
84view.show();
The QGraphicsItem class is the base class for all graphical items in a QGraphicsScene.
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items.
void addItem(QGraphicsItem *item)
Adds or moves the item and all its children to this scene.
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.
QGraphicsTextItem * addText(const QString &text, const QFont &font=QFont())
Creates and adds a text item to the scene, and returns the item pointer.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
void setRenderHints(QPainter::RenderHints hints)
void setBackgroundBrush(const QBrush &brush)
void setCacheMode(CacheMode mode)
void render(QPainter *painter, const QRectF &target=QRectF(), const QRect &source=QRect(), Qt::AspectRatioMode aspectRatioMode=Qt::KeepAspectRatio)
Renders the source rect, which is in view coordinates, from the scene into target,...
\inmodule QtGui
Definition qimage.h:37
qsizetype size() const noexcept
Definition qlist.h:397
\inmodule QtGui
Definition qevent.h:196
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
@ SmoothPixmapTransform
Definition qpainter.h:54
@ Antialiasing
Definition qpainter.h:52
@ HighResolution
Definition qprinter.h:31
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr QRectF adjusted(qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition qrect.h:813
\inmodule QtCore\reentrant
Definition qrect.h:30
void show()
Shows the window.
Definition qwindow.cpp:2254
#define qDebug
[1]
Definition qlogging.h:164
struct _cl_event * event
QGraphicsScene scene
[0]
QGraphicsItem * item
view viewport() -> scroll(dx, dy, deviceRect)
QGraphicsScene scene
[0]
QGraphicsView view
[1]
QList< QTreeWidgetItem * > items
QPainter painter(this)
[7]
QQuickView * view
[0]