1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "analogclock.h"
52
53#include <QPainter>
54#include <QTime>
55#include <QTimer>
56
57//! [0] //! [1]
58AnalogClock::AnalogClock(QWidget *parent)
59//! [0] //! [2]
60 : QWidget(parent)
61//! [2] //! [3]
62{
63//! [3] //! [4]
64 QTimer *timer = new QTimer(this);
65//! [4] //! [5]
66 connect(sender: timer, signal: &QTimer::timeout, receiver: this, slot: QOverload<>::of(ptr: &AnalogClock::update));
67//! [5] //! [6]
68 timer->start(msec: 1000);
69//! [6]
70
71 setWindowTitle(tr(s: "Analog Clock"));
72 resize(w: 200, h: 200);
73//! [7]
74}
75//! [1] //! [7]
76
77//! [8] //! [9]
78void AnalogClock::paintEvent(QPaintEvent *)
79//! [8] //! [10]
80{
81 static const QPoint hourHand[3] = {
82 QPoint(7, 8),
83 QPoint(-7, 8),
84 QPoint(0, -40)
85 };
86 static const QPoint minuteHand[3] = {
87 QPoint(7, 8),
88 QPoint(-7, 8),
89 QPoint(0, -70)
90 };
91
92 QColor hourColor(127, 0, 127);
93 QColor minuteColor(0, 127, 127, 191);
94
95 int side = qMin(a: width(), b: height());
96 QTime time = QTime::currentTime();
97//! [10]
98
99//! [11]
100 QPainter painter(this);
101//! [11] //! [12]
102 painter.setRenderHint(hint: QPainter::Antialiasing);
103//! [12] //! [13]
104 painter.translate(dx: width() / 2, dy: height() / 2);
105//! [13] //! [14]
106 painter.scale(sx: side / 200.0, sy: side / 200.0);
107//! [9] //! [14]
108
109//! [15]
110 painter.setPen(Qt::NoPen);
111//! [15] //! [16]
112 painter.setBrush(hourColor);
113//! [16]
114
115//! [17] //! [18]
116 painter.save();
117//! [17] //! [19]
118 painter.rotate(a: 30.0 * ((time.hour() + time.minute() / 60.0)));
119 painter.drawConvexPolygon(points: hourHand, pointCount: 3);
120 painter.restore();
121//! [18] //! [19]
122
123//! [20]
124 painter.setPen(hourColor);
125//! [20] //! [21]
126
127 for (int i = 0; i < 12; ++i) {
128 painter.drawLine(x1: 88, y1: 0, x2: 96, y2: 0);
129 painter.rotate(a: 30.0);
130 }
131//! [21]
132
133//! [22]
134 painter.setPen(Qt::NoPen);
135//! [22] //! [23]
136 painter.setBrush(minuteColor);
137
138//! [24]
139 painter.save();
140 painter.rotate(a: 6.0 * (time.minute() + time.second() / 60.0));
141 painter.drawConvexPolygon(points: minuteHand, pointCount: 3);
142 painter.restore();
143//! [23] //! [24]
144
145//! [25]
146 painter.setPen(minuteColor);
147//! [25] //! [26]
148
149//! [27]
150 for (int j = 0; j < 60; ++j) {
151 if ((j % 5) != 0)
152 painter.drawLine(x1: 92, y1: 0, x2: 96, y2: 0);
153 painter.rotate(a: 6.0);
154 }
155//! [27]
156}
157//! [26]
158

source code of qtbase/examples/widgets/widgets/analogclock/analogclock.cpp