-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradientqlabel.cpp
More file actions
65 lines (53 loc) · 1.72 KB
/
Copy pathgradientqlabel.cpp
File metadata and controls
65 lines (53 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <QPainter>
#include <QLinearGradient>
#include <QPainterPath>
#include "gradientqlabel.h"
GradientQLabel::GradientQLabel(QWidget *parent)
: QLabel(parent), startColor(Qt::red), endColor(Qt::blue), useGradient(true)
{
}
void GradientQLabel::setGradientColors(const QColor &start, const QColor &end)
{
startColor = start;
endColor = end;
update();
}
void GradientQLabel::setUseGradient(bool enable)
{
useGradient = enable;
update();
}
void GradientQLabel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
if (useGradient) {
// Gradient spans the width of the label
QLinearGradient gradient(0, 0, width(), 0);
gradient.setColorAt(0, startColor);
gradient.setColorAt(1, endColor);
// Build path with the current font
QFont f = font();
f.setStyleStrategy(QFont::ForceOutline);
QPainterPath path;
path.addText(0, fontMetrics().ascent(), f, text());
// Centering offset
QRectF bounds = path.boundingRect();
qreal x = (width() - bounds.width()) / 2.0 - bounds.left();
qreal y = (height() - bounds.height()) / 2.0 - bounds.top();
QTransform transform;
transform.translate(x, y);
path = transform.map(path);
painter.setBrush(gradient);
painter.setPen(Qt::NoPen);
painter.drawPath(path);
} else {
// Normal text rendering
painter.setPen(palette().color(foregroundRole()));
painter.setFont(font());
setAlignment(Qt::AlignCenter);
painter.drawText(rect(), alignment(), text());
}
}