-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
executable file
·79 lines (62 loc) · 2.27 KB
/
clock.cpp
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 <QtWidgets>
#include "clock.h"
Clock::Clock(QWidget *parentWidget)
:QWidget(parentWidget)
{
setWindowTitle(tr("More Clock"));
resize(1000, 500);
m_timer.setParent(this);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
m_timer.start(100);
}
void Clock::paintEvent(QPaintEvent*)
{
int side = qMin(width(), height());
QTime time = QTime::currentTime();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.translate(width() /2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setFont(QFont(tr("Time New Romans"), 12));
for (int hour = 1; hour <= 12; ++hour) {
QString text;
text.setNum(hour);
double angle = (30.0 * hour) - 90;
double length = 90.0;
double x = length * qCos(qDegreesToRadians((angle)));
double y = length * qSin(qDegreesToRadians(angle));
QRect rect(x - 100, y - 100, 200, 200);
painter.drawText(rect, Qt::AlignHCenter | Qt::AlignVCenter, text);
}
double hours = time.hour();
double minutes = time.minute();
double seconds = time.second();
double milliseconds = time.msec();
painter.setPen(Qt::black);
painter.setBrush(Qt::gray);
{
static const QPoint hourHand[3] = {QPoint(8, 8), QPoint(-8, 8), QPoint(0, -60)};
painter.save();
double hour = hours + (minutes / 60.0) + (seconds / 3600.0) + (milliseconds / 3600000.0);
painter.rotate(30.0 * hour);
painter.drawConvexPolygon(hourHand, 3);
painter.restore();
}
{
static const QPoint minuteHand[3] = {QPoint(6, 8), QPoint(-6,8), QPoint(0, -70)};
painter.save();
double minute = minutes + (seconds / 60.0) + (milliseconds / 60000.0);
painter.rotate(6.0 * minute);
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();
}
{
static const QPoint secondHand[3] = {QPoint(4,8), QPoint(-4, 8), QPoint(0, -80)};
painter.save();
double second = seconds + (milliseconds / 1000);
painter.rotate(6.0 * second);
painter.drawConvexPolygon(secondHand, 3);
painter.restore();
}
}