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_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
11{
13public:
15};
16
18{
20public:
22private:
23 qreal m_opacity = 1.0;
24};
25
26//! [0]
27void 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]
39void 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
59void 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}
void example()
[5]