Remove "Analog Clock Window Example"
This is almost exactly the same as the "Analog Clock" (widget) example. "Analog Clock Window Example" demonstrates: * How to render to a QWindow (covered by RasterWindow example) * QPainter and transformations (covered by Analog Clock example) * How to use QTimer (covered by Analog Clock example) Change-Id: I7f20a29798830ed6345eca250e4139cb314cab84 Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io> (cherry picked from commit f7db1cfa92896135534f6ab140d1470e56a5d677) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
7cda805338
commit
1bfa58f11c
@ -44,7 +44,6 @@ manifestmeta.android.names = "Qt3D/Qt 3D: Basic Shapes C++ Example" \
|
|||||||
"QtDataVisualization/Audiolevels Example" \
|
"QtDataVisualization/Audiolevels Example" \
|
||||||
"QtDataVisualization/Qt Quick 2 Scatter Example" \
|
"QtDataVisualization/Qt Quick 2 Scatter Example" \
|
||||||
"QtDataVisualization/Qt Quick 2 Surface Multiseries Example" \
|
"QtDataVisualization/Qt Quick 2 Surface Multiseries Example" \
|
||||||
"QtGui/Analog Clock Window Example" \
|
|
||||||
"QtGui/OpenGL Window Example" \
|
"QtGui/OpenGL Window Example" \
|
||||||
"QtGui/Raster Window Example" \
|
"QtGui/Raster Window Example" \
|
||||||
"QtLinguist/Arrow Pad Example" \
|
"QtLinguist/Arrow Pad Example" \
|
||||||
@ -151,7 +150,6 @@ manifestmeta.android.tags = android
|
|||||||
manifestmeta.ios.names = "QtCore/Contiguous Cache Example" \
|
manifestmeta.ios.names = "QtCore/Contiguous Cache Example" \
|
||||||
"QtCore/Mandelbrot Example" \
|
"QtCore/Mandelbrot Example" \
|
||||||
"QtCore/Queued Custom Type Example" \
|
"QtCore/Queued Custom Type Example" \
|
||||||
"QtGui/Analog Clock Window Example" \
|
|
||||||
"QtGui/OpenGL Window Example" \
|
"QtGui/OpenGL Window Example" \
|
||||||
"QtGui/Raster Window Example" \
|
"QtGui/Raster Window Example" \
|
||||||
"QtNetwork/Loopback Example" \
|
"QtNetwork/Loopback Example" \
|
||||||
|
@ -4,5 +4,4 @@
|
|||||||
if(NOT TARGET Qt6::Gui)
|
if(NOT TARGET Qt6::Gui)
|
||||||
return()
|
return()
|
||||||
endif()
|
endif()
|
||||||
qt_internal_add_example(analogclock)
|
|
||||||
qt_internal_add_example(rasterwindow)
|
qt_internal_add_example(rasterwindow)
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
# Copyright (C) 2022 The Qt Company Ltd.
|
|
||||||
# SPDX-License-Identifier: BSD-3-Clause
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.16)
|
|
||||||
project(analogclock LANGUAGES CXX)
|
|
||||||
|
|
||||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
|
||||||
set(INSTALL_EXAMPLESDIR "examples")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/gui/gui_analogclock")
|
|
||||||
|
|
||||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui)
|
|
||||||
|
|
||||||
qt_standard_project_setup()
|
|
||||||
|
|
||||||
qt_add_executable(gui_analogclock
|
|
||||||
../rasterwindow/rasterwindow.cpp ../rasterwindow/rasterwindow.h
|
|
||||||
main.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
set_target_properties(gui_analogclock PROPERTIES
|
|
||||||
WIN32_EXECUTABLE TRUE
|
|
||||||
MACOSX_BUNDLE TRUE
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(gui_analogclock PRIVATE
|
|
||||||
../rasterwindow
|
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(gui_analogclock PRIVATE
|
|
||||||
Qt6::Core
|
|
||||||
Qt6::Gui
|
|
||||||
)
|
|
||||||
|
|
||||||
install(TARGETS gui_analogclock
|
|
||||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
|
||||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
|
||||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
|
||||||
)
|
|
@ -1,10 +0,0 @@
|
|||||||
include(../rasterwindow/rasterwindow.pri)
|
|
||||||
|
|
||||||
# work-around for QTBUG-13496
|
|
||||||
CONFIG += no_batch
|
|
||||||
|
|
||||||
SOURCES += \
|
|
||||||
main.cpp
|
|
||||||
|
|
||||||
target.path = $$[QT_INSTALL_EXAMPLES]/gui/analogclock
|
|
||||||
INSTALLS += target
|
|
@ -1,123 +0,0 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
|
||||||
|
|
||||||
#include <QtGui>
|
|
||||||
|
|
||||||
#include "rasterwindow.h"
|
|
||||||
|
|
||||||
//! [5]
|
|
||||||
class AnalogClockWindow : public RasterWindow
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
AnalogClockWindow();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void timerEvent(QTimerEvent *) override;
|
|
||||||
void render(QPainter *p) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
int m_timerId;
|
|
||||||
};
|
|
||||||
//! [5]
|
|
||||||
|
|
||||||
|
|
||||||
//! [6]
|
|
||||||
AnalogClockWindow::AnalogClockWindow()
|
|
||||||
{
|
|
||||||
setTitle("Analog Clock");
|
|
||||||
resize(200, 200);
|
|
||||||
|
|
||||||
m_timerId = startTimer(1000);
|
|
||||||
}
|
|
||||||
//! [6]
|
|
||||||
|
|
||||||
//! [7]
|
|
||||||
void AnalogClockWindow::timerEvent(QTimerEvent *event)
|
|
||||||
{
|
|
||||||
if (event->timerId() == m_timerId)
|
|
||||||
renderLater();
|
|
||||||
}
|
|
||||||
//! [7]
|
|
||||||
|
|
||||||
//! [1] //! [14]
|
|
||||||
void AnalogClockWindow::render(QPainter *p)
|
|
||||||
{
|
|
||||||
//! [14]
|
|
||||||
//! [8]
|
|
||||||
static const QPoint hourHand[3] = {
|
|
||||||
QPoint(7, 8),
|
|
||||||
QPoint(-7, 8),
|
|
||||||
QPoint(0, -40)
|
|
||||||
};
|
|
||||||
static const QPoint minuteHand[3] = {
|
|
||||||
QPoint(7, 8),
|
|
||||||
QPoint(-7, 8),
|
|
||||||
QPoint(0, -70)
|
|
||||||
};
|
|
||||||
|
|
||||||
QColor hourColor(127, 0, 127);
|
|
||||||
QColor minuteColor(0, 127, 127, 191);
|
|
||||||
//! [8]
|
|
||||||
|
|
||||||
//! [9]
|
|
||||||
p->setRenderHint(QPainter::Antialiasing);
|
|
||||||
//! [9] //! [10]
|
|
||||||
p->translate(width() / 2, height() / 2);
|
|
||||||
|
|
||||||
int side = qMin(width(), height());
|
|
||||||
p->scale(side / 200.0, side / 200.0);
|
|
||||||
//! [1] //! [10]
|
|
||||||
|
|
||||||
//! [11]
|
|
||||||
p->setPen(Qt::NoPen);
|
|
||||||
p->setBrush(hourColor);
|
|
||||||
//! [11]
|
|
||||||
|
|
||||||
//! [2]
|
|
||||||
QTime time = QTime::currentTime();
|
|
||||||
|
|
||||||
p->save();
|
|
||||||
p->rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
|
||||||
p->drawConvexPolygon(hourHand, 3);
|
|
||||||
p->restore();
|
|
||||||
//! [2]
|
|
||||||
|
|
||||||
//! [12]
|
|
||||||
p->setPen(hourColor);
|
|
||||||
|
|
||||||
for (int i = 0; i < 12; ++i) {
|
|
||||||
p->drawLine(88, 0, 96, 0);
|
|
||||||
p->rotate(30.0);
|
|
||||||
}
|
|
||||||
//! [12] //! [13]
|
|
||||||
p->setPen(Qt::NoPen);
|
|
||||||
p->setBrush(minuteColor);
|
|
||||||
//! [13]
|
|
||||||
|
|
||||||
//! [3]
|
|
||||||
p->save();
|
|
||||||
p->rotate(6.0 * (time.minute() + time.second() / 60.0));
|
|
||||||
p->drawConvexPolygon(minuteHand, 3);
|
|
||||||
p->restore();
|
|
||||||
//! [3]
|
|
||||||
|
|
||||||
//! [4]
|
|
||||||
p->setPen(minuteColor);
|
|
||||||
|
|
||||||
for (int j = 0; j < 60; ++j) {
|
|
||||||
if ((j % 5) != 0)
|
|
||||||
p->drawLine(92, 0, 96, 0);
|
|
||||||
p->rotate(6.0);
|
|
||||||
}
|
|
||||||
//! [4]
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
QGuiApplication app(argc, argv);
|
|
||||||
|
|
||||||
AnalogClockWindow clock;
|
|
||||||
clock.show();
|
|
||||||
|
|
||||||
return app.exec();
|
|
||||||
}
|
|
Binary file not shown.
Before Width: | Height: | Size: 14 KiB |
Binary file not shown.
Before Width: | Height: | Size: 29 KiB |
@ -1,114 +0,0 @@
|
|||||||
// Copyright (C) 2016 The Qt Company Ltd.
|
|
||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\example analogclock
|
|
||||||
\title Analog Clock Window Example
|
|
||||||
|
|
||||||
\brief The Analog Clock Window example shows how to draw the contents of
|
|
||||||
a custom window.
|
|
||||||
|
|
||||||
\image analogclock-window-example.png Screenshot of the Analog
|
|
||||||
Clock Window example
|
|
||||||
|
|
||||||
This example demonstrates how the transformation and scaling
|
|
||||||
features of QPainter can be used to make drawing easier.
|
|
||||||
|
|
||||||
\section1 AnalogClockWindow Class Definition
|
|
||||||
|
|
||||||
The \c AnalogClockWindow class provides a clock with hour and
|
|
||||||
minute hands that is automatically updated every few seconds. We
|
|
||||||
make use of the RasterWindow from the \l {Raster Window Example}
|
|
||||||
and reimplement the \c render function to draw the clock face:
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 5
|
|
||||||
|
|
||||||
\section1 AnalogClock Class Implementation
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 6
|
|
||||||
|
|
||||||
We set a title on the window and resize to a reasonable size. Then
|
|
||||||
we start a timer which we will use to redraw the clock every
|
|
||||||
second.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 7
|
|
||||||
|
|
||||||
The timerEvent function is called every second as a result of
|
|
||||||
our startTimer call. Making use of the convenience in the base
|
|
||||||
class, we schedule the window to be repainted.
|
|
||||||
|
|
||||||
Checking the timer's id is not strictly needed as we only have
|
|
||||||
one active timer in this instance, but it is good practice to do
|
|
||||||
so.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 14
|
|
||||||
\snippet analogclock/main.cpp 8
|
|
||||||
|
|
||||||
Before we set up the painter and draw the clock, we first define
|
|
||||||
two lists of \l {QPoint}s and two \l{QColor}s that will be used
|
|
||||||
for the hour and minute hands. The minute hand's color has an
|
|
||||||
alpha component of 191, meaning that it's 75% opaque.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 9
|
|
||||||
|
|
||||||
We call QPainter::setRenderHint() with QPainter::Antialiasing to
|
|
||||||
turn on antialiasing. This makes drawing of diagonal lines much
|
|
||||||
smoother.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 10
|
|
||||||
|
|
||||||
The translation moves the origin to the center of the window, and
|
|
||||||
the scale operation ensures that the following drawing operations
|
|
||||||
are scaled to fit within the window. We use a scale factor that
|
|
||||||
let's us use x and y coordinates between -100 and 100, and that
|
|
||||||
ensures that these lie within the length of the window's shortest
|
|
||||||
side.
|
|
||||||
|
|
||||||
To make our code simpler, we will draw a fixed size clock face that will
|
|
||||||
be positioned and scaled so that it lies in the center of the window.
|
|
||||||
|
|
||||||
We also determine the length of the window's shortest side so that we
|
|
||||||
can fit the clock face inside the window.
|
|
||||||
|
|
||||||
The painter takes care of all the transformations made during the
|
|
||||||
rendering, and ensures that everything is drawn correctly. Letting
|
|
||||||
the painter handle transformations is often easier than performing
|
|
||||||
manual calculations.
|
|
||||||
|
|
||||||
\image analogclockwindow-viewport.png
|
|
||||||
|
|
||||||
We draw the hour hand first, using a formula that rotates the coordinate
|
|
||||||
system counterclockwise by a number of degrees determined by the current
|
|
||||||
hour and minute. This means that the hand will be shown rotated clockwise
|
|
||||||
by the required amount.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 11
|
|
||||||
|
|
||||||
We set the pen to be Qt::NoPen because we don't want any outline,
|
|
||||||
and we use a solid brush with the color appropriate for
|
|
||||||
displaying hours. Brushes are used when filling in polygons and
|
|
||||||
other geometric shapes.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 2
|
|
||||||
|
|
||||||
We save and restore the transformation matrix before and after the
|
|
||||||
rotation because we want to place the minute hand without having to
|
|
||||||
take into account any previous rotations.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 12
|
|
||||||
|
|
||||||
We draw markers around the edge of the clock for each hour. We
|
|
||||||
draw each marker then rotate the coordinate system so that the
|
|
||||||
painter is ready for the next one.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 13
|
|
||||||
\snippet analogclock/main.cpp 3
|
|
||||||
|
|
||||||
The minute hand is rotated in a similar way to the hour hand.
|
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 4
|
|
||||||
|
|
||||||
Again, we draw markers around the edge of the clock, but this
|
|
||||||
time to indicate minutes. We skip multiples of 5 to avoid drawing
|
|
||||||
minute markers on top of hour markers.
|
|
||||||
*/
|
|
@ -4,5 +4,4 @@ TEMPLATE = subdirs
|
|||||||
QT_FOR_CONFIG += gui
|
QT_FOR_CONFIG += gui
|
||||||
CONFIG += no_docs_target
|
CONFIG += no_docs_target
|
||||||
|
|
||||||
SUBDIRS += analogclock
|
|
||||||
SUBDIRS += rasterwindow
|
SUBDIRS += rasterwindow
|
||||||
|
@ -46,7 +46,6 @@ void AnalogClock::paintEvent(QPaintEvent *)
|
|||||||
QColor minuteColor(0, 127, 127, 191);
|
QColor minuteColor(0, 127, 127, 191);
|
||||||
|
|
||||||
int side = qMin(width(), height());
|
int side = qMin(width(), height());
|
||||||
QTime time = QTime::currentTime();
|
|
||||||
//! [10]
|
//! [10]
|
||||||
|
|
||||||
//! [11]
|
//! [11]
|
||||||
@ -64,8 +63,9 @@ void AnalogClock::paintEvent(QPaintEvent *)
|
|||||||
//! [15] //! [16]
|
//! [15] //! [16]
|
||||||
painter.setBrush(hourColor);
|
painter.setBrush(hourColor);
|
||||||
//! [16]
|
//! [16]
|
||||||
|
//! [18]
|
||||||
//! [17] //! [18]
|
QTime time = QTime::currentTime();
|
||||||
|
//! [17]
|
||||||
painter.save();
|
painter.save();
|
||||||
//! [17] //! [19]
|
//! [17] //! [19]
|
||||||
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
|
||||||
|
@ -62,8 +62,6 @@ imagedirs += images \
|
|||||||
excludefiles += ../kernel/qtestsupport_gui.cpp \
|
excludefiles += ../kernel/qtestsupport_gui.cpp \
|
||||||
../painting/qdrawhelper_ssse3.cpp
|
../painting/qdrawhelper_ssse3.cpp
|
||||||
|
|
||||||
# manifestmeta.highlighted.names = "QtGui/Analog Clock Window Example"
|
|
||||||
|
|
||||||
navigation.landingpage = "Qt GUI"
|
navigation.landingpage = "Qt GUI"
|
||||||
navigation.cppclassespage = "Qt GUI C++ Classes"
|
navigation.cppclassespage = "Qt GUI C++ Classes"
|
||||||
|
|
||||||
|
@ -213,11 +213,11 @@
|
|||||||
\row
|
\row
|
||||||
\li {2,1}
|
\li {2,1}
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 1
|
\snippet ../widgets/widgets/analogclock/analogclock.cpp 9
|
||||||
|
|
||||||
We translate the coordinate system so that point (0, 0) is in the
|
We translate the coordinate system so that point (0, 0) is in the
|
||||||
widget's center, instead of being at the top-left corner. We also
|
widget's center, instead of being at the top-left corner. We also
|
||||||
scale the system by \c side / 100, where \c side is either the
|
scale the system by \c side / 200, where \c side is either the
|
||||||
widget's width or the height, whichever is shortest. We want the
|
widget's width or the height, whichever is shortest. We want the
|
||||||
clock to be square, even if the device isn't.
|
clock to be square, even if the device isn't.
|
||||||
|
|
||||||
@ -227,7 +227,7 @@
|
|||||||
|
|
||||||
See also the \l {Window-Viewport Conversion} section.
|
See also the \l {Window-Viewport Conversion} section.
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 2
|
\snippet ../widgets/widgets/analogclock/analogclock.cpp 18
|
||||||
|
|
||||||
We draw the clock's hour hand by rotating the coordinate system
|
We draw the clock's hour hand by rotating the coordinate system
|
||||||
and calling QPainter::drawConvexPolygon(). Thank's to the
|
and calling QPainter::drawConvexPolygon(). Thank's to the
|
||||||
@ -235,26 +235,35 @@
|
|||||||
|
|
||||||
The polygon is specified as an array of alternating \e x, \e y
|
The polygon is specified as an array of alternating \e x, \e y
|
||||||
values, stored in the \c hourHand static variable (defined at the
|
values, stored in the \c hourHand static variable (defined at the
|
||||||
beginning of the function), which corresponds to the four points
|
beginning of the function), which corresponds to the three points
|
||||||
(2, 0), (0, 2), (-2, 0), and (0, -25).
|
(7, 8), (-7, 8), (0, -40).
|
||||||
|
|
||||||
The calls to QPainter::save() and QPainter::restore() surrounding
|
The calls to QPainter::save() and QPainter::restore() surrounding
|
||||||
the code guarantees that the code that follows won't be disturbed
|
the code guarantees that the code that follows won't be disturbed
|
||||||
by the transformations we've used.
|
by the transformations we've used.
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 3
|
\snippet ../widgets/widgets/analogclock/analogclock.cpp 21
|
||||||
|
|
||||||
|
After that, we draw the hour markers for the clock face, which
|
||||||
|
consists of twelve short lines at 30-degree intervals. When that
|
||||||
|
loop is done, the painter has been rotated a full circle back to
|
||||||
|
its original state, so we don't need to save and restore the state.
|
||||||
|
|
||||||
|
\snippet ../widgets/widgets/analogclock/analogclock.cpp 24
|
||||||
|
|
||||||
We do the same for the clock's minute hand, which is defined by
|
We do the same for the clock's minute hand, which is defined by
|
||||||
the four points (1, 0), (0, 1), (-1, 0), and (0, -40). These
|
the three points (7, 8), (-7, 8), (0, -70). These
|
||||||
coordinates specify a hand that is thinner and longer than the
|
coordinates specify a hand that is thinner and longer than the
|
||||||
minute hand.
|
minute hand.
|
||||||
|
|
||||||
\snippet analogclock/main.cpp 4
|
\snippet ../widgets/widgets/analogclock/analogclock.cpp 27
|
||||||
|
|
||||||
Finally, we draw the clock face, which consists of twelve short
|
Finally, we draw the minute markers for the clock face, which
|
||||||
lines at 30-degree intervals. At the end of that, the painter is
|
consists of sixty short lines at 6-degree intervals. We skip every
|
||||||
rotated in a way which isn't very useful, but we're done with
|
fifth minute marker because we don't want to draw over the hour
|
||||||
painting so that doesn't matter.
|
markers. At the end of that, the painter is rotated in a way which
|
||||||
|
isn't very useful, but we're done with painting so that doesn't
|
||||||
|
matter.
|
||||||
\endtable
|
\endtable
|
||||||
|
|
||||||
For more information about the transformation matrix, see the
|
For more information about the transformation matrix, see the
|
||||||
@ -422,5 +431,5 @@
|
|||||||
\endtable
|
\endtable
|
||||||
\endomit
|
\endomit
|
||||||
|
|
||||||
\sa {Analog Clock Window Example}
|
\sa {Analog Clock}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user