728x90
label = OutlinedLabel(self.test.format("0"), alignment=Qt.AlignCenter, textFormat=Qt.RichText)
label.setPen(QColor(255, 249, 177))
label.setBrush(QColor(171, 171, 173))
label.setStyleSheet("QLabel{background-color: #dbdbdb; border: solid; font-family: 나눔스퀘어; font-size: 32pt; font-weight: bold;}"
"QLabel:hover{background-color: #2d2801;}")
self.ui.horizontalLayout_2.addWidget(label)
class OutlinedLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.w = 1 / 25
self.mode = True
self.setBrush(Qt.white)
self.setPen(Qt.black)
def scaledOutlineMode(self):
return self.mode
def setScaledOutlineMode(self, state):
self.mode = state
def outlineThickness(self):
return self.w * self.font().pointSize() if self.mode else self.w
def setOutlineThickness(self, value):
self.w = value
def setBrush(self, brush):
if not isinstance(brush, QBrush):
brush = QBrush(brush)
self.brush = brush
def setPen(self, pen):
if not isinstance(pen, QPen):
pen = QPen(pen)
pen.setJoinStyle(Qt.RoundJoin)
self.pen = pen
def sizeHint(self):
w = math.ceil(self.outlineThickness() * 2)
return super().sizeHint() + QSize(w, w)
def minimumSizeHint(self):
w = math.ceil(self.outlineThickness() * 2)
return super().minimumSizeHint() + QSize(w, w)
def paintEvent(self, event):
w = self.outlineThickness()
rect = self.rect()
metrics = QFontMetrics(self.font())
tr = metrics.boundingRect(self.text()).adjusted(0, 0, w, w)
if self.indent() == -1:
if self.frameWidth():
indent = (metrics.boundingRect('x').width() + w * 2) / 2
else:
indent = w
else:
indent = self.indent()
if self.alignment() & Qt.AlignLeft:
x = rect.left() + indent - min(metrics.leftBearing(self.text()[0]), 0)
elif self.alignment() & Qt.AlignRight:
x = rect.x() + rect.width() - indent - tr.width()
else:
x = (rect.width() - tr.width()) / 2
if self.alignment() & Qt.AlignTop:
y = rect.top() + indent + metrics.ascent()
elif self.alignment() & Qt.AlignBottom:
y = rect.y() + rect.height() - indent - metrics.descent()
else:
y = (rect.height() + metrics.ascent() - metrics.descent()) / 2
path = QPainterPath()
path.addText(x, y, self.font(), self.text())
qp = QPainter(self)
qp.setRenderHint(QPainter.Antialiasing)
self.pen.setWidthF(w * 2)
qp.strokePath(path, self.pen)
# if 1 < self.brush.style() < 15:
# qp.fillPath(path, self.palette().window())
qp.fillPath(path, self.brush)
qp.end()
https://stackoverflow.com/questions/64290561/qlabel-correct-positioning-for-text-outline
728x90
'파이썬' 카테고리의 다른 글
파이썬 데이터프레임 dataframe 특정 인덱스 행을 마지막으로 옮기기 (0) | 2023.11.02 |
---|---|
fast api crud 만들기 (0) | 2023.10.31 |
pyside, pyqt QGraphicsDropShadowEffect 로 그림자 효과줄 때 (0) | 2023.08.08 |
파이썬 셀레니움 크롬드라이버 ChromeDriverManager 버전 오류 feat.230818 수정 (3) | 2023.07.27 |
파이썬 dict list 특정 key 로 중복제거 (0) | 2023.07.27 |