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_effects_qgraphicseffect.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
<
QGraphicsEffect
>
5
#
include
<
QPainter
>
6
#
include
<
QLinearGradient
>
7
#
include
<
QGraphicsOpacityEffect
>
8
#
include
<
QRect
>
9
10
class
MyGraphicsEffect
:
public
QGraphicsEffect
11
{
12
Q_OBJECT
13
public
:
14
void
draw
(
QPainter
*
painter
)
override
;
15
};
16
17
class
MyGraphicsOpacityEffect
:
public
QGraphicsOpacityEffect
18
{
19
Q_OBJECT
20
public
:
21
void
draw
(
QPainter
*
painter
)
override
;
22
private
:
23
qreal m_opacity = 1.0;
24
};
25
26
//! [0]
27
void
MyGraphicsOpacityEffect
::draw(QPainter *painter)
28
{
29
// Fully opaque; draw directly without going through a pixmap.
30
if
(qFuzzyCompare(m_opacity, 1)) {
31
drawSource(painter);
32
return
;
33
}
34
//...
35
}
36
//! [0]
37
38
//! [1]
39
void
MyGraphicsEffect
::draw(QPainter *painter)
40
{
41
//...
42
QPoint offset;
43
if
(sourceIsPixmap()) {
44
// No point in drawing in device coordinates (pixmap will be scaled anyways).
45
const
QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
46
//...
47
painter->drawPixmap(offset, pixmap);
48
}
else
{
49
// Draw pixmap in device coordinates to avoid pixmap scaling;
50
const
QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset);
51
painter->setWorldTransform(QTransform());
52
//...
53
painter->drawPixmap(offset, pixmap);
54
}
55
//...
56
}
57
//! [1]
58
59
void
example
()
60
{
61
QRect rect;
62
63
//! [2]
64
//...
65
QLinearGradient alphaGradient(rect.topLeft(), rect.bottomLeft());
66
alphaGradient.setColorAt(0.0, Qt::transparent);
67
alphaGradient.setColorAt(0.5, Qt::black);
68
alphaGradient.setColorAt(1.0, Qt::transparent);
69
QGraphicsOpacityEffect *effect =
new
QGraphicsOpacityEffect;
70
effect->setOpacityMask(alphaGradient);
71
//...
72
//! [2]
73
}
MyGraphicsEffect
Definition
src_gui_effects_qgraphicseffect.cpp:11
MyGraphicsOpacityEffect
Definition
src_gui_effects_qgraphicseffect.cpp:18
example
void example()
[5]
Definition
doc_src_groups.cpp:7
qtbase
src
widgets
doc
snippets
code
src_gui_effects_qgraphicseffect.cpp
Generated on
for Qt by
1.18.0