diff options
author | Friedemann Kleint <[email protected]> | 2024-12-17 10:40:47 +0100 |
---|---|---|
committer | Friedemann Kleint <[email protected]> | 2025-02-06 09:56:05 +0100 |
commit | 8f452f3d4d863f77f2b5054a4f17c25034b1b339 (patch) | |
tree | 5ec81f0078f615e43dfb692de618eb5f4b42e6fa | |
parent | 38912923dfa6ae6f119dec56b3b11ceedbc81e1b (diff) |
Use new 6.9 API in examples
Task-number: PYSIDE-2862
Change-Id: I45f0cafa5276ed7c387b903962f2845a23c327e8
Reviewed-by: Ece Cinucen <[email protected]>
Reviewed-by: Cristian Maureira-Fredes <[email protected]>
-rw-r--r-- | examples/gui/analogclock/main.py | 28 | ||||
-rw-r--r-- | examples/widgets/itemviews/stardelegate/starrating.py | 43 | ||||
-rw-r--r-- | examples/widgets/painting/basicdrawing/basicdrawing.py | 78 | ||||
-rw-r--r-- | examples/widgets/tutorials/cannon/t11.py | 13 | ||||
-rw-r--r-- | examples/widgets/tutorials/cannon/t12.py | 13 | ||||
-rw-r--r-- | examples/widgets/tutorials/cannon/t13.py | 13 | ||||
-rw-r--r-- | examples/widgets/tutorials/cannon/t14.py | 15 |
7 files changed, 96 insertions, 107 deletions
diff --git a/examples/gui/analogclock/main.py b/examples/gui/analogclock/main.py index 28ce3454c..10ed66e3f 100644 --- a/examples/gui/analogclock/main.py +++ b/examples/gui/analogclock/main.py @@ -5,7 +5,8 @@ from __future__ import annotations import sys from PySide6.QtCore import QPoint, QTimer, QTime, Qt -from PySide6.QtGui import QGuiApplication, QPainter, QPalette, QPolygon, QRasterWindow +from PySide6.QtGui import (QGuiApplication, QPainter, QPainterStateGuard, + QPalette, QPolygon, QRasterWindow) """Simplified PySide6 port of the gui/analogclock example from Qt v6.x""" @@ -54,10 +55,9 @@ class AnalogClockWindow(QRasterWindow): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(self._hour_color) - painter.save() - painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) - painter.drawConvexPolygon(self._hour_hand) - painter.restore() + with QPainterStateGuard(painter): + painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) + painter.drawConvexPolygon(self._hour_hand) for _ in range(0, 12): painter.drawRect(73, -3, 16, 6) @@ -65,19 +65,17 @@ class AnalogClockWindow(QRasterWindow): painter.setBrush(self._minute_color) - painter.save() - painter.rotate(6.0 * time.minute()) - painter.drawConvexPolygon(self._minute_hand) - painter.restore() + with QPainterStateGuard(painter): + painter.rotate(6.0 * time.minute()) + painter.drawConvexPolygon(self._minute_hand) painter.setBrush(self._seconds_color) - painter.save() - painter.rotate(6.0 * time.second()) - painter.drawConvexPolygon(self._seconds_hand) - painter.drawEllipse(-3, -3, 6, 6) - painter.drawEllipse(-5, -68, 10, 10) - painter.restore() + with QPainterStateGuard(painter): + painter.rotate(6.0 * time.second()) + painter.drawConvexPolygon(self._seconds_hand) + painter.drawEllipse(-3, -3, 6, 6) + painter.drawEllipse(-5, -68, 10, 10) painter.setPen(self._minute_color) diff --git a/examples/widgets/itemviews/stardelegate/starrating.py b/examples/widgets/itemviews/stardelegate/starrating.py index 694bb43eb..96c08fca7 100644 --- a/examples/widgets/itemviews/stardelegate/starrating.py +++ b/examples/widgets/itemviews/stardelegate/starrating.py @@ -6,7 +6,7 @@ from __future__ import annotations from math import (cos, sin, pi) -from PySide6.QtGui import (QPainter, QPolygonF) +from PySide6.QtGui import (QPainter, QPainterStateGuard, QPolygonF) from PySide6.QtCore import (QPointF, QSize, Qt) PAINTING_SCALE_FACTOR = 20 @@ -39,25 +39,22 @@ class StarRating: def paint(self, painter, rect, palette, isEditable=False): """ Paint the stars (and/or diamonds if we're in editing mode). """ - painter.save() - - painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) - painter.setPen(Qt.PenStyle.NoPen) - - if isEditable: - painter.setBrush(palette.highlight()) - else: - painter.setBrush(palette.windowText()) - - y_offset = (rect.height() - PAINTING_SCALE_FACTOR) / 2 - painter.translate(rect.x(), rect.y() + y_offset) - painter.scale(PAINTING_SCALE_FACTOR, PAINTING_SCALE_FACTOR) - - for i in range(self.MAX_STAR_COUNT): - if i < self.star_count: - painter.drawPolygon(self._star_polygon, Qt.FillRule.WindingFill) - elif isEditable: - painter.drawPolygon(self._diamond_polygon, Qt.WindingFill) - painter.translate(1.0, 0.0) - - painter.restore() + with QPainterStateGuard(painter): + painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) + painter.setPen(Qt.NoPen) + + if isEditable: + painter.setBrush(palette.highlight()) + else: + painter.setBrush(palette.windowText()) + + y_offset = (rect.height() - PAINTING_SCALE_FACTOR) / 2 + painter.translate(rect.x(), rect.y() + y_offset) + painter.scale(PAINTING_SCALE_FACTOR, PAINTING_SCALE_FACTOR) + + for i in range(self.MAX_STAR_COUNT): + if i < self.star_count: + painter.drawPolygon(self._star_polygon, Qt.FillRule.WindingFill) + elif isEditable: + painter.drawPolygon(self._diamond_polygon, Qt.FillRule.WindingFill) + painter.translate(1.0, 0.0) diff --git a/examples/widgets/painting/basicdrawing/basicdrawing.py b/examples/widgets/painting/basicdrawing/basicdrawing.py index ef4af5d85..a712c5803 100644 --- a/examples/widgets/painting/basicdrawing/basicdrawing.py +++ b/examples/widgets/painting/basicdrawing/basicdrawing.py @@ -7,8 +7,8 @@ from __future__ import annotations from PySide6.QtCore import QPoint, QRect, QSize, Qt, qVersion from PySide6.QtGui import (QBrush, QConicalGradient, QLinearGradient, QPainter, - QPainterPath, QPalette, QPen, QPixmap, QPolygon, - QRadialGradient) + QPainterStateGuard, QPainterPath, QPalette, QPen, + QPixmap, QPolygon, QRadialGradient) from PySide6.QtWidgets import (QApplication, QCheckBox, QComboBox, QGridLayout, QLabel, QSpinBox, QWidget) @@ -86,44 +86,42 @@ class RenderArea(QWidget): for x in range(0, self.width(), 100): for y in range(0, self.height(), 100): - painter.save() - painter.translate(x, y) - if self.transformed: - painter.translate(50, 50) - painter.rotate(60.0) - painter.scale(0.6, 0.9) - painter.translate(-50, -50) - - if self.shape == RenderArea.Line: - painter.drawLine(rect.bottomLeft(), rect.topRight()) - elif self.shape == RenderArea.Points: - painter.drawPoints(RenderArea.points) - elif self.shape == RenderArea.Polyline: - painter.drawPolyline(RenderArea.points) - elif self.shape == RenderArea.Polygon: - painter.drawPolygon(RenderArea.points) - elif self.shape == RenderArea.Rect: - painter.drawRect(rect) - elif self.shape == RenderArea.RoundedRect: - painter.drawRoundedRect(rect, 25, 25, Qt.SizeMode.RelativeSize) - elif self.shape == RenderArea.Ellipse: - painter.drawEllipse(rect) - elif self.shape == RenderArea.Arc: - painter.drawArc(rect, start_angle, arc_length) - elif self.shape == RenderArea.Chord: - painter.drawChord(rect, start_angle, arc_length) - elif self.shape == RenderArea.Pie: - painter.drawPie(rect, start_angle, arc_length) - elif self.shape == RenderArea.Path: - painter.drawPath(path) - elif self.shape == RenderArea.Text: - qv = qVersion() - painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, - f"PySide 6\nQt {qv}") - elif self.shape == RenderArea.Pixmap: - painter.drawPixmap(10, 10, self.pixmap) - - painter.restore() + with QPainterStateGuard(painter): + painter.translate(x, y) + if self.transformed: + painter.translate(50, 50) + painter.rotate(60.0) + painter.scale(0.6, 0.9) + painter.translate(-50, -50) + + if self.shape == RenderArea.Line: + painter.drawLine(rect.bottomLeft(), rect.topRight()) + elif self.shape == RenderArea.Points: + painter.drawPoints(RenderArea.points) + elif self.shape == RenderArea.Polyline: + painter.drawPolyline(RenderArea.points) + elif self.shape == RenderArea.Polygon: + painter.drawPolygon(RenderArea.points) + elif self.shape == RenderArea.Rect: + painter.drawRect(rect) + elif self.shape == RenderArea.RoundedRect: + painter.drawRoundedRect(rect, 25, 25, Qt.SizeMode.RelativeSize) + elif self.shape == RenderArea.Ellipse: + painter.drawEllipse(rect) + elif self.shape == RenderArea.Arc: + painter.drawArc(rect, start_angle, arc_length) + elif self.shape == RenderArea.Chord: + painter.drawChord(rect, start_angle, arc_length) + elif self.shape == RenderArea.Pie: + painter.drawPie(rect, start_angle, arc_length) + elif self.shape == RenderArea.Path: + painter.drawPath(path) + elif self.shape == RenderArea.Text: + qv = qVersion() + painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, + f"PySide 6\nQt {qv}") + elif self.shape == RenderArea.Pixmap: + painter.drawPixmap(10, 10, self.pixmap) painter.setPen(self.palette().dark().color()) painter.setBrush(Qt.BrushStyle.NoBrush) diff --git a/examples/widgets/tutorials/cannon/t11.py b/examples/widgets/tutorials/cannon/t11.py index 00ac2fc26..6ba2d1a19 100644 --- a/examples/widgets/tutorials/cannon/t11.py +++ b/examples/widgets/tutorials/cannon/t11.py @@ -9,7 +9,7 @@ import sys import math from PySide6.QtCore import QPoint, QRect, QTimer, Qt, Signal, Slot, qWarning -from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion +from PySide6.QtGui import QColor, QFont, QPainter, QPainterStateGuard, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLCDNumber, QPushButton, QSlider, QVBoxLayout, QWidget) @@ -138,12 +138,11 @@ class CannonField(QWidget): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(Qt.GlobalColor.blue) - painter.save() - painter.translate(0, self.height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(CannonField.barrel_rect) - painter.restore() + with QPainterStateGuard(painter): + painter.translate(0, self.height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(CannonField.barrel_rect) def cannon_rect(self): result = QRect(0, 0, 50, 50) diff --git a/examples/widgets/tutorials/cannon/t12.py b/examples/widgets/tutorials/cannon/t12.py index 4960797bc..c503f9d5e 100644 --- a/examples/widgets/tutorials/cannon/t12.py +++ b/examples/widgets/tutorials/cannon/t12.py @@ -10,7 +10,7 @@ import math import random from PySide6.QtCore import QPoint, QRect, QTime, QTimer, Qt, Signal, Slot, qWarning -from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion +from PySide6.QtGui import QColor, QFont, QPainter, QPainterStateGuard, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSlider, QVBoxLayout, QWidget) @@ -184,12 +184,11 @@ class CannonField(QWidget): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(Qt.GlobalColor.blue) - painter.save() - painter.translate(0, self.height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(CannonField.barrel_rect) - painter.restore() + with QPainterStateGuard(painter): + painter.translate(0, self.height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(CannonField.barrel_rect) def cannon_rect(self): result = QRect(0, 0, 50, 50) diff --git a/examples/widgets/tutorials/cannon/t13.py b/examples/widgets/tutorials/cannon/t13.py index c9b8bd5d7..39912fd19 100644 --- a/examples/widgets/tutorials/cannon/t13.py +++ b/examples/widgets/tutorials/cannon/t13.py @@ -11,7 +11,7 @@ import random from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, Qt, Signal, Slot, qWarning) -from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion +from PySide6.QtGui import QColor, QFont, QPainter, QPainterStateGuard, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSizePolicy, QSlider, QVBoxLayout, QWidget) @@ -211,12 +211,11 @@ class CannonField(QWidget): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(Qt.GlobalColor.blue) - painter.save() - painter.translate(0, self.height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(CannonField.barrel_rect) - painter.restore() + with QPainterStateGuard(painter): + painter.translate(0, self.height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(CannonField.barrel_rect) def cannon_rect(self): result = QRect(0, 0, 50, 50) diff --git a/examples/widgets/tutorials/cannon/t14.py b/examples/widgets/tutorials/cannon/t14.py index 00674189b..d78243489 100644 --- a/examples/widgets/tutorials/cannon/t14.py +++ b/examples/widgets/tutorials/cannon/t14.py @@ -11,8 +11,8 @@ import random from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, QSize, Qt, Signal, Slot, qWarning) -from PySide6.QtGui import (QColor, QFont, QKeySequence, QPainter, QPalette, - QShortcut, QRegion, QTransform) +from PySide6.QtGui import (QColor, QFont, QKeySequence, QPainter, QPainterStateGuard, + QPalette, QShortcut, QRegion, QTransform) from PySide6.QtWidgets import (QApplication, QFrame, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSizePolicy, QSlider, QVBoxLayout, QWidget) @@ -241,12 +241,11 @@ class CannonField(QWidget): painter.setPen(Qt.PenStyle.NoPen) painter.setBrush(Qt.GlobalColor.blue) - painter.save() - painter.translate(0, self.height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(CannonField.barrel_rect) - painter.restore() + with QPainterStateGuard(painter): + painter.translate(0, self.height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(CannonField.barrel_rect) def cannon_rect(self): result = QRect(0, 0, 50, 50) |