-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
executable file
·73 lines (59 loc) · 2.05 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
#include <QtWidgets>
#include "clock.h"
Clock::Clock(QWidget *parentWidgetPtr)
:QWidget(parentWidgetPtr)
{
setWindowTitle(tr("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);
for (int second = 0; second <= 60; ++second) {
if ((second % 5 ) == 0) {
painter.drawLine(QPoint(0, 81), QPoint(0, 98));
} else {
painter.drawLine(QPoint(0, 90), QPoint(0, 98));
}
painter.rotate(6);
}
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();
}
}