from PySide6.QtWidgets import QLabel from PySide6.QtGui import QPainter, QColor from PySide6.QtCore import Qt class OutlinedLabel(QLabel): def __init__(self, text='', outline_color=QColor(0, 0, 0), outline_width=2, *args, **kwargs): super().__init__(*args, **kwargs) self.setText(text) self.outline_color = outline_color self.outline_width = outline_width def paintEvent(self, event): painter = QPainter(self) painter.setFont(self.font()) painter.setRenderHint(QPainter.RenderHint.Antialiasing) # 绘制描边 painter.setPen(self.outline_color) for dx in range(-self.outline_width, self.outline_width + 1): for dy in range(-self.outline_width, self.outline_width + 1): if abs(dx) + abs(dy) <= self.outline_width: painter.drawText(self.rect().adjusted(dx, dy, dx, dy), Qt.AlignmentFlag.AlignCenter, self.text()) # 绘制文本 painter.setPen(self.palette().windowText().color()) painter.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, self.text())