diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-09-15 15:59:12 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-09-16 14:13:37 +0200 |
commit | d59bc5fecc27a4ada48834783ec5f739b6037a09 (patch) | |
tree | e9dc0a92098fa53d1e3862bc8abc8147359d65bc /examples/gui | |
parent | cf32b66adbfb489cd6e5d5c0bf3f741b59ba204c (diff) |
Add the QtGui analogclock example
Uses the ready-made QRasterWindow instead of the QWindow with backing
store of the C++ version for simplicity.
Task-number: PYSIDE-841
Change-Id: Ib0146b1563405b97f694cc53b382c1188061d6af
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
Diffstat (limited to 'examples/gui')
-rw-r--r-- | examples/gui/analogclock/analogclock.pyproject | 3 | ||||
-rw-r--r-- | examples/gui/analogclock/doc/analogclock.rst | 8 | ||||
-rw-r--r-- | examples/gui/analogclock/main.py | 77 |
3 files changed, 88 insertions, 0 deletions
diff --git a/examples/gui/analogclock/analogclock.pyproject b/examples/gui/analogclock/analogclock.pyproject new file mode 100644 index 000000000..cc7a74a34 --- /dev/null +++ b/examples/gui/analogclock/analogclock.pyproject @@ -0,0 +1,3 @@ +{ + "files": ["main.py"] +} diff --git a/examples/gui/analogclock/doc/analogclock.rst b/examples/gui/analogclock/doc/analogclock.rst new file mode 100644 index 000000000..a1ede41dd --- /dev/null +++ b/examples/gui/analogclock/doc/analogclock.rst @@ -0,0 +1,8 @@ +Analog Clock Window Example +=========================== + +The Analog Clock Window example shows how to draw the contents of +a custom window. + +This example demonstrates how the transformation and scaling +features of QPainter can be used to make drawing easier. diff --git a/examples/gui/analogclock/main.py b/examples/gui/analogclock/main.py new file mode 100644 index 000000000..12303cd1d --- /dev/null +++ b/examples/gui/analogclock/main.py @@ -0,0 +1,77 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QPoint, QTimer, QTime, Qt +from PySide6.QtGui import (QColor, QGradient, QGuiApplication, QPainter, + QPolygon, QRasterWindow) + +"""Simplified PySide6 port of the gui/analogclock example from Qt v6.x""" + + +class AnalogClockWindow(QRasterWindow): + + def __init__(self): + super().__init__() + self.setTitle("Analog Clock") + self.resize(200, 200) + + self._timer = QTimer(self) + self._timer.timeout.connect(self.update) + self._timer.start(1000) + + self._hour_hand = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -40)]) + self._minute_hand = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -70)]) + + self._hour_color = QColor(127, 0, 127) + self._minute_color = QColor(0, 127, 127, 191) + + def paintEvent(self, e): + with QPainter(self) as p: + self.render(p) + + def render(self, p): + width = self.width() + height = self.height() + p.fillRect(0, 0, width, height, QGradient.NightFade) + + p.setRenderHint(QPainter.Antialiasing) + p.translate(width / 2, height / 2) + + side = min(width, height) + p.scale(side / 200.0, side / 200.0) + p.setPen(Qt.NoPen) + p.setBrush(self._hour_color) + time = QTime.currentTime() + + p.save() + p.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) + p.drawConvexPolygon(self._hour_hand) + p.restore() + p.setPen(self._hour_color) + + for i in range(0, 12): + p.drawLine(88, 0, 96, 0) + p.rotate(30.0) + + p.setPen(Qt.NoPen) + p.setBrush(self._minute_color) + + p.save() + p.rotate(6.0 * (time.minute() + time.second() / 60.0)) + p.drawConvexPolygon(self._minute_hand) + p.restore() + p.setPen(self._minute_color) + + for j in range(0, 60): + if (j % 5) != 0: + p.drawLine(92, 0, 96, 0) + p.rotate(6.0) + + +if __name__ == "__main__": + app = QGuiApplication(sys.argv) + clock = AnalogClockWindow() + clock.show() + sys.exit(app.exec()) |