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) line_count = 0 block = self.edit.document().begin() while block.isValid(): line_count += 1 position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft() if position.y() > page_bottom: break bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) self.current = line_count painter.drawText(self.width() - font_metrics.width(str(line_count)) - 10, round(position.y()) - contents_y + font_metrics.ascent(), str(line_count)) if bold: font = painter.font() font.setBold(False) painter.setFont(font) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
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) line_count = 0 block = self.edit.document().begin() while block.isValid(): line_count += 1 position = self.edit.document().documentLayout().blockBoundingRect( block).topLeft() if position.y() > page_bottom: break bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) self.current = line_count painter.drawText( self.width() - font_metrics.width(str(line_count)) - 10, round(position.y()) - contents_y + font_metrics.ascent(), str(line_count)) if bold: font = painter.font() font.setBold(False) painter.setFont(font) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
def paint( self, painter: QPainter, rect: QRect, mode: QIcon.Mode, state: QIcon.State ): """override""" font = FIconEngine.font if hasattr(FIconEngine, "font") else painter.font() # The following test is to avoid crash when running python widget outside the __main__.my if not font: font = painter.font() return painter.save() if self.color: painter.setPen(QPen(self.color)) else: if mode == QIcon.Disabled: painter.setPen( QPen(self.palette.color(QPalette.Disabled, QPalette.ButtonText)) ) else: painter.setPen(QPen(self.palette.color(QPalette.Active, QPalette.Text))) font.setPixelSize(rect.size().width()) painter.setFont(font) # painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.drawText( rect, Qt.AlignCenter | Qt.AlignVCenter, str(chr(self.hex_character)) ) painter.restore()
def paintEvent(self, event): p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) r = event.rect() if self.is_revealed: if self.is_start: p.drawPixmap(r, QPixmap(IMG_START)) elif self.is_mine: p.drawPixmap(r, QPixmap(IMG_BOMB)) elif self.adjacent_n > 0: pen = QPen(NUM_COLORS[self.adjacent_n]) p.setPen(pen) f = p.font() f.setBold(True) p.setFont(f) p.drawText(r, Qt.AlignHCenter | Qt.AlignVCenter, str(self.adjacent_n)) else: p.fillRect(r, QBrush(Qt.lightGray)) pen = QPen(Qt.gray) pen.setWidth(1) p.setPen(pen) p.drawRect(r) if self.is_flagged: p.drawPixmap(r, QPixmap(IMG_FLAG))
def drawText(self, painter: QPainter, index: int, offset: int) -> None: painter.save() width: int = self.width() height: int = self.height() strValue: str = str(self.__listValue[index]) target: int = width if self.__horizontal else height font: QFont = painter.font() font.setPixelSize((target - abs(offset)) // 8) painter.setFont(font) if self.__horizontal: textWidth: int = painter.fontMetrics().width(strValue) initX: int = width // 2 + offset - textWidth // 2 painter.drawText(QRect(initX, 0, textWidth, height), Qt.AlignCenter, strValue) # 计算最后中间值停留的起始坐标,以便鼠标松开时矫正居中 if index is self.__currentIndex: self.__currentPos = initX else: textHeight: int = painter.fontMetrics().height() initY: int = height // 2 + offset - textHeight // 2 painter.drawText(QRect(0, initY, width, textHeight), Qt.AlignCenter, strValue) # 计算最后中间值停留的起始坐标,以便鼠标松开时矫正居中 if index is self.__currentIndex: self.__currentPos = initY painter.restore()
def _paintEvent(self, event): super().paintEvent(event) painter = QPainter(self) fontMetrics = painter.fontMetrics() lineSpacing = self.fontMetrics().lineSpacing() y = 0 textLayout = QTextLayout(self._content, painter.font()) textLayout.setTextOption(self._textOption) textLayout.beginLayout() while True: line = textLayout.createLine() if not line.isValid(): break line.setLineWidth(self.width()) nextLineY = y + lineSpacing if self.height() >= nextLineY + lineSpacing: # not the last line elidedLine = self._content[line.textStart():line.textStart() + line.textLength()] elidedLine = fontMetrics.elidedText(elidedLine, self._elideMode, self.width()) painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLine) y = nextLineY else: # last line, check if we are to elide here to the end lastLine = self._content[line.textStart():] elidedLastLine = fontMetrics.elidedText(lastLine, self._elideMode, self.width()) painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine) break textLayout.endLayout()
def paintEvent(self, paintEvent): painter = QPainter(self) font = painter.font() font.setPixelSize(14) font.setBold(True) painter.setFont(font) # clear background painter.setBackground(QColor("#1D212D")) painter.eraseRect(self.rect()) self.apply_camera(painter) self.draw_links(painter) self.draw_rooms(painter) self.draw_solution_paths(painter) self.draw_ants(painter) # reset transform painter.resetMatrix() self.draw_room_names(painter)
def draw_text(expected: QPainter, x: int, y: int, text: str): window = expected.window() scene = QGraphicsScene(0, 0, window.width(), window.height()) text_item = scene.addSimpleText(text) font = expected.font() text_item.setFont(font) center_text_item(text_item, x, y) scene.render(expected)
def paintEvent(self, event): painter = QPainter() painter.begin(self) painter.setRenderHint(painter.Antialiasing) font = painter.font() font.setBold(True) painter.setFont(font) x = self.rect().x() y = self.rect().y() w = self.rect().width() - 1 h = self.rect().height() - 1 # draw the text painter.drawText(x + 33, y + 3, w, 16, Qt.AlignLeft | Qt.AlignTop, self.title()) painter.setRenderHint(QPainter.Antialiasing, False) self.__drawTriangle(painter, x, y) # draw the borders - top headerHeight = 20 headerRect = QRect(x + 1, y + 1, w - 1, headerHeight) headerRectShadow = QRect(x - 1, y - 1, w + 1, headerHeight + 2) # Highlight pen = QPen(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(headerRect) painter.fillRect(headerRect, QColor(255, 255, 255, 18)) # Shadow pen.setColor(self.palette().color(QPalette.Dark)) painter.setPen(pen) painter.drawRect(headerRectShadow) if not self.isCollapsed(): # draw the lover border pen = QPen(self.palette().color(QPalette.Dark)) pen.setWidthF(0.8) painter.setPen(pen) offSet = headerHeight + 3 bodyRect = QRect(x, y + offSet, w, h - offSet) bodyRectShadow = QRect(x + 1, y + offSet, w + 1, h - offSet + 1) painter.drawRect(bodyRect) pen.setColor(self.palette().color(QPalette.Light)) pen.setWidthF(0.4) painter.setPen(pen) painter.drawRect(bodyRectShadow) painter.end()
def paintEvent(self, event: PySide2.QtGui.QPaintEvent): painter = QPainter(self) if self.orientation is None: return if self.image is not None: painter.save() image_width = self.image.width() image_height = self.image.height() if self.orientation % 2 == 0: scale = min(self.width() / image_width, self.height() / image_height) else: scale = min(self.width() / image_height, self.height() / image_width) target_width = image_width * scale target_height = image_height * scale padding_left = (self.width() - target_width) // 2 padding_top = (self.height() - target_height) // 2 painter.translate(self.width() / 2, self.height() / 2) painter.rotate(90 * self.orientation) painter.translate(-self.width() / 2, -self.height() / 2) painter.drawImage( QRect(padding_left, padding_top, target_width, target_height), self.image) painter.restore() if self.original_orientation is not None: painter.save() self.render_orientation_indicator(painter, self.original_orientation, target_height, target_width, pen_color=Qt.yellow, brush_color=Qt.darkYellow) painter.restore() if self.suggested_orientation is not None: painter.save() self.render_orientation_indicator(painter, self.suggested_orientation, target_height, target_width, pen_color=Qt.green, brush_color=Qt.darkGreen) painter.restore() if self.loading: painter.save() painter.setPen(self.palette().brush(QPalette.Foreground).color()) font = painter.font() font.setPointSize(font.pointSize() * 2) painter.setFont(font) draw_text(painter, self.width() / 2, self.height() / 2, Qt.AlignVCenter | Qt.AlignHCenter, "Loading...") painter.restore()
def paintEvent(self, event): # pylint: disable=invalid-name, unused-argument p = QPainter(self) p.setRenderHint(QPainter.Antialiasing, True) p.setPen(Qt.NoPen) track_opacity = self._track_opacity thumb_opacity = 1.0 text_opacity = 1.0 if self.isEnabled(): track_brush = self._track_color[self.isChecked()] thumb_brush = self._thumb_color[self.isChecked()] text_color = self._text_color[self.isChecked()] else: track_opacity *= 0.8 track_brush = self.palette().shadow() thumb_brush = self.palette().mid() text_color = self.palette().shadow().color() p.setBrush(track_brush) p.setOpacity(track_opacity) p.drawRoundedRect( self._margin, self._margin, self.width() - 2 * self._margin, self.height() - 2 * self._margin, self._track_radius, self._track_radius, ) p.setBrush(thumb_brush) p.setOpacity(thumb_opacity) p.drawEllipse( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ) p.setPen(text_color) p.setOpacity(text_opacity) font = p.font() font.setPixelSize(1.5 * self._thumb_radius) p.setFont(font) p.drawText( QRectF( self.offset - self._thumb_radius, self._base_offset - self._thumb_radius, 2 * self._thumb_radius, 2 * self._thumb_radius, ), Qt.AlignCenter, self._thumb_text[self.isChecked()], )
def paintEvent(self, ev): """ Manually implemented paint event :param ev: the QT paint event :return: """ h = self.height() w = self.width() p = QPainter(self) p.setClipRect(ev.region().boundingRect()) pen = QPen(QColor(0, 0, 0)) pen.setWidth(4) ls = QFontMetricsF(p.font()).lineSpacing() for idx, t in enumerate(sorted(list(self._loadData.keys()))): y = 10 + idx * ls pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawLine(QLineF(15, y, 15 + 15, y)) pen.setColor(QColor(0, 0, 0)) p.setPen(pen) p.drawText(QPointF(35, y), t) if len(self._loadData) > 0: right = max([ polygon[polygon.count() - 1].x() for _, polygon in self._loadData.items() ]) else: right = 0.0 p.translate(w - 10 - right * 20, h - 10) p.scale( 20, -(h - 20) ) # x direction: 20 pixels per second, y direction: spread between 10 and h-10 topleft = p.transform().inverted()[0].map(QPointF(10, 10)) pen.setWidthF(0) pen.setCosmetic(True) left = topleft.x() p.setRenderHint(QPainter.Antialiasing, True) p.setPen(pen) p.drawLine(QLineF(left, 0, right, 0)) p.drawLine(QLineF(left, 0, left, 1)) idx = 0 for t, polygon in self._loadData.items(): pen.setColor(ThreadToColor.singleton.get(t)) p.setPen(pen) p.drawPolyline(polygon) p.end()
def paint(self, painter: QPainter, rect: QRect, mode: QIcon.Mode, state: QIcon.State): """override""" font = FIconEngine.font if hasattr(FIconEngine, "font") else painter.font() painter.save() if mode == QIcon.Disabled: painter.setPen( QPen(qApp.palette().color(QPalette.Disabled, QPalette.Text))) else: painter.setPen(QPen(self.color)) font.setPixelSize(rect.size().width()) painter.setFont(font) # painter.setRenderHint(QPainter.HighQualityAntialiasing, True) painter.drawText(rect, Qt.AlignCenter | Qt.AlignVCenter, str(chr(self.hex_character))) painter.restore()
def paintEvent(self, event): """ standard qt paint event :param event: a QPaintEvent instance :return: """ super().paintEvent(event) painter = QPainter(self) try: fontMetrics = painter.fontMetrics() lineSpacing = self.fontMetrics().lineSpacing() y = 0 textLayout = QTextLayout(self._content, painter.font()) textLayout.setTextOption(self._textOption) textLayout.beginLayout() while True: line = textLayout.createLine() if not line.isValid(): break line.setLineWidth(self.width()) nextLineY = y + lineSpacing if self.height() >= nextLineY + lineSpacing: # not the last line elidedLine = self._content[line.textStart():line.textStart() + line.textLength()] elidedLine = fontMetrics.elidedText(elidedLine, self._elideMode, self.width()) painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLine) y = nextLineY else: # last line, check if we are to elide here to the end lastLine = self._content[line.textStart():] elidedLastLine = fontMetrics.elidedText(lastLine, self._elideMode, self.width()) painter.drawText(QPoint(0, y + fontMetrics.ascent()), elidedLastLine) break textLayout.endLayout() except Exception as e: logger.exception("Exception during paint: %s", e)
def paintEvent(self, ev): """ Manually implemented paint event of the time / occupancy diagram. :param ev: the qt paint event :return: """ bgcolor = self.palette().color(self.backgroundRole()) h = self.height() w = self.width() p = QPainter(self) p.setClipRect(ev.region().boundingRect()) pen = QPen(QColor(0, 0, 0)) pen.setWidth(0) pen.setCosmetic(True) ls = QFontMetricsF(p.font()).lineSpacing() maxx = 0 minx = None for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): sd = self._spanData[t][port] maxx = np.maximum(maxx, np.max(sd)) minx = np.minimum( minx, np.min(sd)) if minx is not None else np.min(sd) scalex = 1e-9 * 200 # 200 pixels / second # (maxx-minx)*scalex + offx = w-10 if minx is None: return offx = w - 10 - (maxx - minx) * scalex idx = 0 self.portYCoords = [] for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): pen.setColor(QColor(0, 0, 0)) p.setPen(pen) y = 10 + idx * ls self.portYCoords.append((t, port, y - ls / 2, y)) idx += 1 sd = self._spanData[t][port] for i in range(sd.shape[0]): x1, x2 = sd[i, :] x1 = (x1 - minx) * scalex + offx x2 = (x2 - minx) * scalex + offx color = ThreadToColor.singleton.get(t) color.setAlpha(125) p.fillRect(QRectF(x1, y - ls / 2, x2 - x1, ls / 2), color) p.drawRect(QRectF(x1, y - ls / 2, x2 - x1, ls / 2)) pen = QPen(QColor(40, 40, 40)) pen.setWidth(0) pen.setCosmetic(True) pen.setStyle(Qt.DashLine) p.setPen(pen) for x in range(w - 10, -1, -20): p.drawLine(x, 10, x, h - 10) idx = 0 pen.setStyle(Qt.SolidLine) p.setPen(pen) for t in sorted(list(self._spanData.keys())): for port in sorted(list(self._spanData[t].keys())): y = 10 + idx * ls idx += 1 br = QFontMetricsF(p.font()).boundingRect(port) br.translate(10, y) p.fillRect(br, bgcolor) p.drawText(10, y, port) p.end()
############ QApplication.setAttribute(Qt.AA_ShareOpenGLContexts) # needed when a plugin initializes a Qt WebEngine app = QApplication(sys.argv) # get root widget rootWidget = app.desktop() ################# # init main form # the UI is not loaded yet window = Form1() ################# # init global QPainter for paint event qp = QPainter() # stuff for Before/After tags qp.font = QFont("Arial", 8) qp.markPath = QPainterPath() qp.markRect = QRect(0, 0, 50, 20) qp.markPath.addRoundedRect(qp.markRect, 5, 5) def paintEvent(widg, e, qp=qp): """ Paint event handler. It displays the presentation layer of a vImage object in a Qlabel, with current offset and zooming coefficient. The widget must have a valid img attribute of type vImage. The handler should be used to override the paintEvent method of widg. This can be done by subclassing (not directly compatible with Qt Designer) or by assigning paintEvent to the method widg.paintEvent (cf. the function set_event_handler below). @param widg: widget