From b61f735acd8fa2e43a68d7d90f977d8f1506052a Mon Sep 17 00:00:00 2001 From: Christian Tismer Date: Wed, 26 Jan 2022 12:49:43 +0100 Subject: 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 --- examples/widgets/codeeditor/codeeditor.py | 39 ++++++++++++++----------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'examples/widgets/codeeditor') diff --git a/examples/widgets/codeeditor/codeeditor.py b/examples/widgets/codeeditor/codeeditor.py index bfe92c4a8..47dc8b0a3 100644 --- a/examples/widgets/codeeditor/codeeditor.py +++ b/examples/widgets/codeeditor/codeeditor.py @@ -85,29 +85,26 @@ class CodeEditor(QPlainTextEdit): self.line_number_area.setGeometry(rect) def lineNumberAreaPaintEvent(self, event): - painter = QPainter(self.line_number_area) - painter.fillRect(event.rect(), Qt.lightGray) - block = self.firstVisibleBlock() - block_number = block.blockNumber() - offset = self.contentOffset() - top = self.blockBoundingGeometry(block).translated(offset).top() - bottom = top + self.blockBoundingRect(block).height() - - while block.isValid() and top <= event.rect().bottom(): - if block.isVisible() and bottom >= event.rect().top(): - number = str(block_number + 1) - painter.setPen(Qt.black) - width = self.line_number_area.width() - height = self.fontMetrics().height() - painter.drawText(0, top, width, height, Qt.AlignRight, number) - - block = block.next() - top = bottom + with QPainter(self.line_number_area) as painter: + painter.fillRect(event.rect(), Qt.lightGray) + block = self.firstVisibleBlock() + block_number = block.blockNumber() + offset = self.contentOffset() + top = self.blockBoundingGeometry(block).translated(offset).top() bottom = top + self.blockBoundingRect(block).height() - block_number += 1 - # QPainter needs an explicit end() in PyPy. This will become a context manager in 6.3. - painter.end() + while block.isValid() and top <= event.rect().bottom(): + if block.isVisible() and bottom >= event.rect().top(): + number = str(block_number + 1) + painter.setPen(Qt.black) + width = self.line_number_area.width() + height = self.fontMetrics().height() + painter.drawText(0, top, width, height, Qt.AlignRight, number) + + block = block.next() + top = bottom + bottom = top + self.blockBoundingRect(block).height() + block_number += 1 @Slot() def update_line_number_area_width(self, newBlockCount): -- cgit v1.2.3