diff options
| author | Christian Tismer <tismer@stackless.com> | 2022-01-26 12:49:43 +0100 |
|---|---|---|
| committer | Christian Tismer <tismer@stackless.com> | 2022-01-26 16:47:13 +0000 |
| commit | b61f735acd8fa2e43a68d7d90f977d8f1506052a (patch) | |
| tree | 9a5f4fb9debe1d7d51119ea9e169e58bc47bc62f /examples/widgets/state-machine/rogue/rogue.py | |
| parent | dc2046124f132ba0187d1bff97364448288b1cd6 (diff) | |
examples: Turn most QPainter instances into context managers
After the new context manager is in place, most of
the examples benefit from moving QPainter into a
`with` statement.
The comments concerning PyPy could be removed, again.
[ChangeLog][PySide6] The examples are updated to use the new
context manager for QPainter.
Task-number: PYSIDE-535
Change-Id: Idf7e1f734d549ed663383ffbb2416297ebb1e0c7
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/widgets/state-machine/rogue/rogue.py')
| -rw-r--r-- | examples/widgets/state-machine/rogue/rogue.py | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/examples/widgets/state-machine/rogue/rogue.py b/examples/widgets/state-machine/rogue/rogue.py index 255785f95..19394542a 100644 --- a/examples/widgets/state-machine/rogue/rogue.py +++ b/examples/widgets/state-machine/rogue/rogue.py @@ -161,27 +161,25 @@ class MainWindow(QMainWindow): def paintEvent(self, event): metrics = QFontMetrics(self.font()) - painter = QPainter(self) - font_height = metrics.height() - font_width = metrics.horizontalAdvance('X') - - painter.fillRect(self.rect(), Qt.black) - painter.setPen(Qt.white) - - y_pos = font_height - painter.drawText(QPoint(0, y_pos), self.status) - for y in range(self.height): - y_pos += font_height - x_pos = 0 - for x in range(self.width): - if y == self.pY and x == self.pX: + with QPainter(self) as painter: + font_height = metrics.height() + font_width = metrics.horizontalAdvance('X') + + painter.fillRect(self.rect(), Qt.black) + painter.setPen(Qt.white) + + y_pos = font_height + painter.drawText(QPoint(0, y_pos), self.status) + for y in range(self.height): + y_pos += font_height + x_pos = 0 + for x in range(self.width): + if y == self.pY and x == self.pX: + x_pos += font_width + continue + painter.drawText(QPoint(x_pos, y_pos), self.map[x][y]) x_pos += font_width - continue - painter.drawText(QPoint(x_pos, y_pos), self.map[x][y]) - x_pos += font_width - painter.drawText(QPoint(self.pX * font_width, (self.pY + 2) * font_height), '@') - # QPainter needs an explicit end() in PyPy. This will become a context manager in 6.3. - painter.end() + painter.drawText(QPoint(self.pX * font_width, (self.pY + 2) * font_height), '@') def move_player(self, direction): if direction == self.left: |
