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/tetrix/tetrix.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/tetrix/tetrix.py')
| -rw-r--r-- | examples/widgets/tetrix/tetrix.py | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/examples/widgets/tetrix/tetrix.py b/examples/widgets/tetrix/tetrix.py index c70e634b4..04a8b5f57 100644 --- a/examples/widgets/tetrix/tetrix.py +++ b/examples/widgets/tetrix/tetrix.py @@ -212,32 +212,30 @@ class TetrixBoard(QFrame): def paintEvent(self, event): super(TetrixBoard, self).paintEvent(event) - painter = QPainter(self) - rect = self.contentsRect() + with QPainter(self) as painter: + rect = self.contentsRect() - if self._is_paused: - painter.drawText(rect, Qt.AlignCenter, "Pause") - return + if self._is_paused: + painter.drawText(rect, Qt.AlignCenter, "Pause") + return - board_top = rect.bottom() - TetrixBoard.board_height * self.square_height() + board_top = rect.bottom() - TetrixBoard.board_height * self.square_height() - for i in range(TetrixBoard.board_height): - for j in range(TetrixBoard.board_width): - shape = self.shape_at(j, TetrixBoard.board_height - i - 1) - if shape != Piece.NoShape: - self.draw_square(painter, - rect.left() + j * self.square_width(), - board_top + i * self.square_height(), shape) - - if self._cur_piece.shape() != Piece.NoShape: - for i in range(4): - x = self._cur_x + self._cur_piece.x(i) - y = self._cur_y - self._cur_piece.y(i) - self.draw_square(painter, rect.left() + x * self.square_width(), - board_top + (TetrixBoard.board_height - y - 1) * self.square_height(), - self._cur_piece.shape()) - # QPainter needs an explicit end() in PyPy. This will become a context manager in 6.3. - painter.end() + for i in range(TetrixBoard.board_height): + for j in range(TetrixBoard.board_width): + shape = self.shape_at(j, TetrixBoard.board_height - i - 1) + if shape != Piece.NoShape: + self.draw_square(painter, + rect.left() + j * self.square_width(), + board_top + i * self.square_height(), shape) + + if self._cur_piece.shape() != Piece.NoShape: + for i in range(4): + x = self._cur_x + self._cur_piece.x(i) + y = self._cur_y - self._cur_piece.y(i) + self.draw_square(painter, rect.left() + x * self.square_width(), + board_top + (TetrixBoard.board_height - y - 1) * self.square_height(), + self._cur_piece.shape()) def keyPressEvent(self, event): if not self._is_started or self._is_paused or self._cur_piece.shape() == Piece.NoShape: @@ -359,10 +357,8 @@ class TetrixBoard(QFrame): dy = self._next_piece.max_y() - self._next_piece.min_y() + 1 pixmap = QPixmap(dx * self.square_width(), dy * self.square_height()) - painter = QPainter(pixmap) - painter.fillRect(pixmap.rect(), self.nextPieceLabel.palette().background()) - # QPainter needs an explicit end() in PyPy. This will become a context manager in 6.3. - painter.end() + with QPainter(pixmap) as painter: + painter.fillRect(pixmap.rect(), self.nextPieceLabel.palette().background()) for int in range(4): x = self._next_piece.x(i) - self._next_piece.min_x() |
