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
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
4//! [0]
6scene.addText("Hello, world!");
7
8QGraphicsView view(&scene);
9view.show();
10//! [0]
11
12
13//! [1]
15scene.addItem(...
16...
17QPrinter printer(QPrinter::HighResolution);
18printer.setPaperSize(QPrinter::A4);
19
20QPainter painter(&printer);
21scene.render(&painter);
22//! [1]
23
24
25//! [2]
26QSizeF segmentSize = sceneRect().size() / pow(2, depth - 1);
27//! [2]
28
29
30//! [3]
31QGraphicsScene scene;
32QGraphicsView view(&scene);
33view.show();
34
35// a blue background
36scene.setBackgroundBrush(Qt::blue);
37
38// a gradient background
39QRadialGradient gradient(0, 0, 10);
40gradient.setSpread(QGradient::RepeatSpread);
41scene.setBackgroundBrush(gradient);
42//! [3]
43
44
45//! [4]
46QGraphicsScene scene;
47QGraphicsView view(&scene);
48view.show();
49
50// a white semi-transparent foreground
51scene.setForegroundBrush(QColor(255, 255, 255, 127));
52
53// a grid foreground
54scene.setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern));
55//! [4]
56
57
58//! [5]
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}
85//! [5]
QGraphicsScene scene
[0]
QQuickView * view
[0]