Advertisement
StSav012

PySide6.QtGui.QPainter misses points

Feb 3rd, 2025 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | Source Code | 0 0
  1. from typing import Sequence, overload
  2.  
  3. from PySide6.QtCore import QPoint, QPointF, qVersion
  4. from PySide6.QtGui import QPaintDevice, QPaintEngine, QPainter, QPolygon
  5.  
  6.  
  7. class PaintEngine(QPaintEngine):
  8.     def drawPoints(
  9.         self,
  10.         points: QPointF | QPoint | Sequence[QPointF | QPoint],
  11.         *args: int,
  12.         **kwargs: int,
  13.     ) -> None:
  14.         print("drawPoints:")
  15.         if not isinstance(points, Sequence):
  16.             print("a single point is passed:", points)
  17.         else:
  18.             print(len(points), "points passed", points)
  19.  
  20.     def drawPolygon(
  21.         self,
  22.         points: QPointF | QPoint | Sequence[QPointF | QPoint],
  23.         *args: QPaintEngine.PolygonDrawMode | int,
  24.         **kwargs: QPaintEngine.PolygonDrawMode | int,
  25.     ) -> None:
  26.         print("drawPolygon:")
  27.         if not isinstance(points, Sequence):
  28.             print("a single point is passed:", points)
  29.         else:
  30.             print(len(points), "points passed", points)
  31.  
  32.     def begin(self, _dev: QPaintDevice) -> bool:
  33.         return True
  34.  
  35.     def end(self) -> bool:
  36.         return True
  37.  
  38.  
  39. class PaintDevice(QPaintDevice):
  40.     def __init__(self) -> None:
  41.         super().__init__()
  42.         self._engine: PaintEngine = PaintEngine()
  43.  
  44.     def paintEngine(self) -> PaintEngine:
  45.         return self._engine
  46.  
  47.     def metric(self, metric: QPaintDevice.PaintDeviceMetric) -> int:
  48.         if metric == QPaintDevice.PaintDeviceMetric.PdmDevicePixelRatioScaled:
  49.             return super().metric(metric)
  50.         return 1
  51.  
  52.  
  53. print("Qt", qVersion())
  54. pd: PaintDevice = PaintDevice()
  55. polygon: Sequence[QPoint | QPointF] = [QPoint(1, 2), QPoint(3, 4)]
  56. painter: QPainter
  57. with QPainter(pd) as painter:
  58.     painter.drawPoints(polygon)
  59.     painter.drawPolyline(polygon)
  60.     painter.drawPolyline(QPolygon(polygon))
  61.     painter.drawPolygon(polygon)
  62.     painter.drawPolygon(QPolygon(polygon))
  63.  
  64.  
  65. """
  66. Expected output:
  67. ```
  68. Qt 6.8.2
  69. drawPoints:
  70. 2 points passed (PyQt6.QtCore.QPoint(1, 2), PyQt6.QtCore.QPoint(3, 4))
  71. drawPolygon:
  72. 2 points passed (PyQt6.QtCore.QPoint(1, 2), PyQt6.QtCore.QPoint(3, 4))
  73. drawPolygon:
  74. 2 points passed (PyQt6.QtCore.QPoint(1, 2), PyQt6.QtCore.QPoint(3, 4))
  75. drawPolygon:
  76. 2 points passed (PyQt6.QtCore.QPoint(1, 2), PyQt6.QtCore.QPoint(3, 4))
  77. drawPolygon:
  78. 2 points passed (PyQt6.QtCore.QPoint(1, 2), PyQt6.QtCore.QPoint(3, 4))
  79. ```
  80.  
  81. Actual output:
  82. ```
  83. Qt 6.8.2
  84. drawPoints:
  85. a single point is passed: PySide6.QtCore.QPoint(1, 2)
  86. drawPolygon:
  87. a single point is passed: PySide6.QtCore.QPoint(1, 2)
  88. drawPolygon:
  89. a single point is passed: PySide6.QtCore.QPoint(1, 2)
  90. drawPolygon:
  91. a single point is passed: PySide6.QtCore.QPoint(1, 2)
  92. drawPolygon:
  93. a single point is passed: PySide6.QtCore.QPoint(1, 2)
  94. ```
  95. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement