def paintEvent(self, event): contents_y = self.edit.verticalScrollBar().value() page_bottom = contents_y + self.edit.viewport().height() font_metrics = self.fontMetrics() current_block = self.edit.document().findBlock( self.edit.textCursor().position()) painter = QPainter(self) painter.setPen(Qt.darkGray) line_count = 0 first_pos = -1 posdiff = 0 # Iterate over all text blocks in the document. block = self.edit.document().begin() while block.isValid(): line_count += 1 # the top left position of the block in the document if posdiff == 0: position_y = self.edit.document().documentLayout( ).blockBoundingRect(block).topLeft().y() if first_pos == -1: first_pos = position_y else: posdiff = position_y - first_pos else: position_y += posdiff # check if the position of the block is out side of visible area if position_y > page_bottom: break # we want the line number for the selected line to be bold. bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) painter.setPen(Qt.black) # Draw the line number right justified at the y position of the # line. 3 is the magic padding number. drawText(x, y, text) painter.drawText( self.width() - font_metrics.width(str(line_count)) - 3, position_y - contents_y + font_metrics.ascent(), str(line_count)) if bold: font = painter.font() font.setBold(False) painter.setFont(font) painter.setPen(Qt.darkGray) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
def paintEvent(self, event): painter = QPainter(self) # Hello world # ブラシ(塗りつぶし)の色を黒に painter.setBrush(Qt.black) # ペン(線描画)の色も黒に painter.setPen(Qt.black) # 背景を描く # self.rect()はwidgetのサイズを返すので、widget全体を埋める四角形を描画する # ペンとブラシの色が黒なので背景色は真っ黒 painter.drawRect(self.rect()) # ペン(線描画)の色を白にする painter.setPen(Qt.white) # フォントサイズを変更する font = painter.font() # つらさはできるだけ大きく表現したほうが良い # 周りが気づくように font.setPointSize(80) painter.setFont(font) # テキストを描画する x = 0 # 左端 y = self.rect().height() * 0.5 # 上下の中心 painter.drawText(x, y, "TSU☆RA☆I")
def paintEvent(self, event): contents_y = self.edit.verticalScrollBar().value() page_bottom = contents_y + self.edit.viewport().height() font_metrics = self.fontMetrics() current_block = self.edit.document().findBlock(self.edit.textCursor().position()) painter = QPainter(self) painter.setPen(Qt.darkGray) line_count = 0 # Iterate over all text blocks in the document. block = self.edit.document().begin() while block.isValid(): line_count += 1 # the top left position of the block in the document position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft() # check if the position of the block is out side of visible area if position.y() > page_bottom: break # we want the line number for the selected line to be bold. bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) painter.setPen(Qt.black) # Draw the line number right justified at the y position of the # line. 3 is the magic padding number. drawText(x, y, text) painter.drawText( self.width() - font_metrics.width(str(line_count)) - 3, round(position.y()) - contents_y + font_metrics.ascent() + self.edit.document().documentMargin(), str(line_count), ) if bold: font = painter.font() font.setBold(False) painter.setFont(font) painter.setPen(Qt.darkGray) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)