Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
#
include
<
QtWidgets
>
5
#
include
<
QPrinter
>
6
7
void
examples
()
8
{
9
{
10
//! [0]
11
QGraphicsScene scene;
12
scene.addText(
"Hello, world!"
);
13
14
QGraphicsView view(&scene);
15
view.show();
16
//! [0]
17
}
18
19
{
20
QGraphicsItem *someItem;
21
22
//! [1]
23
QGraphicsScene scene;
24
scene.addItem(someItem);
25
//...
26
QPrinter printer(QPrinter::HighResolution);
27
printer.setPageSize(QPageSize::A4);
28
29
QPainter painter(&printer);
30
scene.render(&painter);
31
//! [1]
32
}
33
34
{
35
int
depth;
36
auto
sceneRect = []() {
return
QRectF(); };
37
38
//! [2]
39
QSizeF segmentSize = sceneRect().size() / pow(2, depth - 1);
40
//! [2]
41
}
42
43
{
44
//! [3]
45
QGraphicsScene scene;
46
QGraphicsView view(&scene);
47
view.show();
48
49
// a blue background
50
scene.setBackgroundBrush(Qt::blue);
51
52
// a gradient background
53
QRadialGradient gradient(0, 0, 10);
54
gradient.setSpread(QGradient::RepeatSpread);
55
scene.setBackgroundBrush(gradient);
56
//! [3]
57
}
58
59
{
60
//! [4]
61
QGraphicsScene scene;
62
QGraphicsView view(&scene);
63
view.show();
64
65
// a white semi-transparent foreground
66
scene.setForegroundBrush(QColor(255, 255, 255, 127));
67
68
// a grid foreground
69
scene.setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern));
70
//! [4]
71
}
72
}
73
74
class
TileScene
:
public
QGraphicsScene
75
{
76
public
:
77
TileScene
(
int
numTilesH,
int
numTilesV,
int
tileWidth,
int
tileHeight, QObject *parent =
nullptr
);
78
79
QRectF
rectForTile
(
int
x,
int
y)
const
;
80
void
setTile
(
int
x,
int
y,
const
QPixmap &pixmap);
81
82
protected
:
83
void
drawBackground
(QPainter *painter,
const
QRectF &exposed)
override
;
84
85
private
:
86
int
numTilesH;
87
int
numTilesV;
88
int
tileWidth;
89
int
tileHeight;
90
QVector<QVector<QPixmap>> tiles;
91
};
92
93
//! [5]
94
QRectF
TileScene
::
rectForTile
(
int
x,
int
y)
const
95
{
96
// Return the rectangle for the tile at position (x, y).
97
return
QRectF(x * tileWidth, y * tileHeight, tileWidth, tileHeight);
98
}
99
100
void
TileScene
::
setTile
(
int
x,
int
y,
const
QPixmap &pixmap)
101
{
102
// Sets or replaces the tile at position (x, y) with pixmap.
103
if
(x >= 0 && x < numTilesH && y >= 0 && y < numTilesV) {
104
tiles[y][x] = pixmap;
105
invalidate(rectForTile(x, y), BackgroundLayer);
106
}
107
}
108
109
void
TileScene
::
drawBackground
(QPainter *painter,
const
QRectF &exposed)
110
{
111
// Draws all tiles that intersect the exposed area.
112
for
(
int
y = 0; y < numTilesV; ++y) {
113
for
(
int
x = 0; x < numTilesH; ++x) {
114
QRectF rect = rectForTile(x, y);
115
if
(exposed.intersects(rect))
116
painter->drawPixmap(rect.topLeft(), tiles[y][x]);
117
}
118
}
119
}
120
//! [5]
TileScene
Definition
src_gui_graphicsview_qgraphicsscene.cpp:75
TileScene::rectForTile
QRectF rectForTile(int x, int y) const
[5]
Definition
src_gui_graphicsview_qgraphicsscene.cpp:94
TileScene::TileScene
TileScene(int numTilesH, int numTilesV, int tileWidth, int tileHeight, QObject *parent=nullptr)
TileScene::setTile
void setTile(int x, int y, const QPixmap &pixmap)
Definition
src_gui_graphicsview_qgraphicsscene.cpp:100
TileScene::drawBackground
void drawBackground(QPainter *painter, const QRectF &exposed) override
Draws the background of the scene using painter, before any items and the foreground are drawn.
Definition
src_gui_graphicsview_qgraphicsscene.cpp:109
examples
bool examples()
[3]
Definition
src_corelib_global_qglobal.cpp:56
qtbase
src
widgets
doc
snippets
code
src_gui_graphicsview_qgraphicsscene.cpp
Generated on
for Qt by
1.18.0