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_qgraphicsscene.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
11
12
15scene.addItem(...
16...
18printer.setPaperSize(QPrinter::A4);
19
20QPainter painter(&printer);
23
24
26QSizeF segmentSize = sceneRect().size() / pow(2, depth - 1);
28
29
33view.show();
34
35// a blue background
37
38// a gradient background
39QRadialGradient gradient(0, 0, 10);
40gradient.setSpread(QGradient::RepeatSpread);
43
44
48view.show();
49
50// a white semi-transparent foreground
51scene.setForegroundBrush(QColor(255, 255, 255, 127));
52
53// a grid foreground
56
57
59QRectF TileScene::rectForTile(int x, int y) const
60{
61 // Return the rectangle for the tile at position (x, y).
62 return QRectF(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
63}
64
65void TileScene::setTile(int x, int y, const QPixmap &pixmap)
66{
67 // Sets or replaces the tile at position (x, y) with pixmap.
68 if (x >= 0 && x < numTilesH && y >= 0 && y < numTilesV) {
69 tiles[y][x] = pixmap;
70 invalidate(rectForTile(x, y), BackgroundLayer);
71 }
72}
73
74void TileScene::drawBackground(QPainter *painter, const QRectF &exposed)
75{
76 // Draws all tiles that intersect the exposed area.
77 for (int y = 0; y < numTilesV; ++y) {
78 for (int x = 0; x < numTilesH; ++x) {
79 QRectF rect = rectForTile(x, y);
80 if (exposed.intersects(rect))
81 painter->drawPixmap(rect.topLeft(), tiles[y][x]);
82 }
83 }
84}
\inmodule QtGui
Definition qbrush.h:30
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
@ RepeatSpread
Definition qbrush.h:149
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.
void setForegroundBrush(const QBrush &brush)
void setBackgroundBrush(const QBrush &brush)
QGraphicsTextItem * addText(const QString &text, const QFont &font=QFont())
Creates and adds a text 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.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device.
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\reentrant
Definition qprinter.h:28
@ HighResolution
Definition qprinter.h:31
\inmodule QtGui
Definition qbrush.h:412
\inmodule QtCore\reentrant
Definition qrect.h:484
bool intersects(const QRectF &r) const noexcept
Returns true if this rectangle intersects with the given rectangle (i.e.
Definition qrect.cpp:2271
\inmodule QtCore
Definition qsize.h:208
void show()
Shows the window.
Definition qwindow.cpp:2254
rect
[4]
@ blue
Definition qnamespace.h:37
@ lightGray
Definition qnamespace.h:34
@ CrossPattern
GLint GLint GLint GLint GLint x
[0]
GLint GLenum GLsizei GLsizei GLsizei depth
GLint y
QGraphicsScene scene
[0]
widget render & pixmap
QPainter painter(this)
[7]
QQuickView * view
[0]