-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlednumber.cpp
More file actions
79 lines (63 loc) · 1.44 KB
/
lednumber.cpp
File metadata and controls
79 lines (63 loc) · 1.44 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "lednumber.h"
#include <QPainter>
#include <QPainterPath>
#include <QTimer>
LEDNumber::LEDNumber(QWidget *parent)
: QWidget(parent), _bgColor(Qt::blue), _font("楷体"), _content(52)
{
}
void LEDNumber::start()
{
_t = new QTimer(this);
connect(_t, &QTimer::timeout, this, &LEDNumber::refresh);
_t->setInterval(1000);
_t->start();
}
void LEDNumber::stop()
{
disconnect(_t, &QTimer::timeout, this, &LEDNumber::refresh);
_t->stop();
}
void LEDNumber::setColor(const QColor &bgColor, const QColor &fontColor)
{
_bgColor = bgColor;
_fontColor = fontColor;
}
void LEDNumber::setFont(const QFont &font)
{
_font = font;
}
void LEDNumber::setContent(const QString &content)
{
_content = content;
}
void LEDNumber::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
draw();
}
void LEDNumber::draw()
{
// Draw background
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(_bgColor);
QPainterPath path;
path.addRoundedRect(rect(), 10, 10);
p.fillPath(path, _bgColor);
p.drawPath(path);
//Draw middle line
QPen pen(Qt::white, 5);
p.setPen(pen);
p.drawLine(0, height()/2, width(), height()/2);
// Draw content
p.setPen(_fontColor);
p.setFont(QFont("楷体", 80, 36));
p.drawText(rect(), Qt::AlignCenter, _content);
}
void LEDNumber::refresh()
{
int num = qrand() % 1024;
setContent(QString::number(num));
repaint();
}