PyQt5 - How to change QPushButton QLabel?
-
I'm trying to outline the text on a
QPushButton
. I found this to make an outlined label but now i need to set that label as theQPushButton
one.This code will create an outlined label:
import sys import math from PyQt5.QtWidgets import QLabel, QWidget, QApplication, QVBoxLayout from PyQt5.QtGui import QPen, QBrush, QFontMetrics, QPainterPath, QPainter from PyQt5.QtCore import Qt, QSize 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, int(w), int(w)) # DeprecationWarning: an integer is required (got type float). 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 if w * 2 > 0 else 0) # Otherwise QPen::setWidthF: Setting a pen width with a negative value is not defined qp.strokePath(path, self.pen) if 1 < self.brush.style() < 15: qp.fillPath(path, self.palette().window()) qp.fillPath(path, self.brush) class Window(QWidget): def __init__(self): super().__init__() self.setLayout(QVBoxLayout()) label = OutlinedLabel('Hello world') label.setStyleSheet("font-size: 50px;") self.layout().addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
-
I'm trying to outline the text on a
QPushButton
. I found this to make an outlined label but now i need to set that label as theQPushButton
one.This code will create an outlined label:
import sys import math from PyQt5.QtWidgets import QLabel, QWidget, QApplication, QVBoxLayout from PyQt5.QtGui import QPen, QBrush, QFontMetrics, QPainterPath, QPainter from PyQt5.QtCore import Qt, QSize 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, int(w), int(w)) # DeprecationWarning: an integer is required (got type float). 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 if w * 2 > 0 else 0) # Otherwise QPen::setWidthF: Setting a pen width with a negative value is not defined qp.strokePath(path, self.pen) if 1 < self.brush.style() < 15: qp.fillPath(path, self.palette().window()) qp.fillPath(path, self.brush) class Window(QWidget): def __init__(self): super().__init__() self.setLayout(QVBoxLayout()) label = OutlinedLabel('Hello world') label.setStyleSheet("font-size: 50px;") self.layout().addWidget(label) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
You can define a QPushButton as parent widget of OutlinedLabel:
class Window(QWidget): def __init__(self): super().__init__() self.setLayout(QVBoxLayout()) button = QPushButton() button = QPushButton(self) label = OutlinedLabel('Hello world', button) label.setStyleSheet("font-size: 50px;") button.clicked.connect(lambda: print("Button pressed!")) button.setFixedSize(label.sizeHint()) self.layout().addWidget(button)
-
You can define a QPushButton as parent widget of OutlinedLabel:
class Window(QWidget): def __init__(self): super().__init__() self.setLayout(QVBoxLayout()) button = QPushButton() button = QPushButton(self) label = OutlinedLabel('Hello world', button) label.setStyleSheet("font-size: 50px;") button.clicked.connect(lambda: print("Button pressed!")) button.setFixedSize(label.sizeHint()) self.layout().addWidget(button)
Thank you @ndias , it worked. 🙃