Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
analogclock.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QtWidgets>
5
6#include "analogclock.h"
7
8// QTimer
10AnalogClock::AnalogClock(QWidget *parent)
12 : QWidget(parent)
14{
16 QTimer *timer = new QTimer(this);
18 connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
20 timer->start(1000);
22
23 setWindowTitle(tr("Analog Clock"));
24 resize(200, 200);
26}
28
30AnalogClock::AnalogClock(QWidget *parent)
31
32 : QWidget(parent)
33{
34 auto *timer = new QChronoTimer(1s, this);
35 connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update));
36 timer->start();
37 ...
38 ...
39 setWindowTitle(tr("Analog Clock"));
40 resize(200, 200);
41}
43
44void AnalogClock::paintEvent(QPaintEvent *)
45{
46 static const QPoint hourHand[3] = {
47 QPoint(7, 8),
48 QPoint(-7, 8),
49 QPoint(0, -40)
50 };
51 static const QPoint minuteHand[3] = {
52 QPoint(7, 8),
53 QPoint(-7, 8),
54 QPoint(0, -70)
55 };
56
57 QColor hourColor(127, 0, 127);
58 QColor minuteColor(0, 127, 127, 191);
59
60 int side = qMin(width(), height());
62
63 QPainter painter(this);
65 painter.translate(width() / 2, height() / 2);
66 painter.scale(side / 200.0, side / 200.0);
67
69 painter.setBrush(hourColor);
70
71 painter.save();
72 painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
73 painter.drawConvexPolygon(hourHand, 3);
75
76 painter.setPen(hourColor);
77
78 for (int i = 0; i < 12; ++i) {
79 painter.drawLine(88, 0, 96, 0);
80 painter.rotate(30.0);
81 }
82
84 painter.setBrush(minuteColor);
85
86 painter.save();
87 painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
88 painter.drawConvexPolygon(minuteHand, 3);
90
91 painter.setPen(minuteColor);
92
93 for (int j = 0; j < 60; ++j) {
94 if ((j % 5) != 0)
95 painter.drawLine(92, 0, 96, 0);
96 painter.rotate(6.0);
97 }
98}
\inmodule QtCore
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void drawConvexPolygon(const QPointF *points, int pointCount)
Draws the convex polygon defined by the first pointCount points in the array points using the current...
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void drawLine(const QLineF &line)
Draws a line defined by line.
Definition qpainter.h:442
void restore()
Restores the current painter state (pops a saved state off the stack).
void rotate(qreal a)
Rotates the coordinate system clockwise.
void scale(qreal sx, qreal sy)
Scales the coordinate system by ({sx}, {sy}).
void save()
Saves the current painter state (pushes the state onto a stack).
void setBrush(const QBrush &brush)
Sets the painter's brush to the given brush.
@ Antialiasing
Definition qpainter.h:52
void translate(const QPointF &offset)
Translates the coordinate system by the given offset; i.e.
void setRenderHint(RenderHint hint, bool on=true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint.
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore \reentrant
Definition qdatetime.h:215
static QTime currentTime()
Returns the current time as reported by the system clock.
int hour() const
Returns the hour part (0 to 23) of the time.
int minute() const
Returns the minute part (0 to 59) of the time.
int second() const
Returns the second part (0 to 59) of the time.
\inmodule QtCore
Definition qtimer.h:20
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
Definition qtimer.cpp:241
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
a resize(100000)
@ NoPen
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
GLint GLsizei GLsizei height
GLint GLsizei width
GLdouble s
[6]
Definition qopenglext.h:235
#define tr(X)
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QTimer * timer
[3]
QPainter painter(this)
[7]