问题
I have this code
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
class ThinLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def paintEvent(self, event):
qp = QPainter(self)
qp.setRenderHint(QPainter.Antialiasing)
path = QPainterPath()
path.addText(event.rect().bottomLeft(), self.font(), self.text())
qp.setPen(QPen(self.palette().color(QPalette.Window), 2))
qp.setBrush(self.palette().text())
qp.drawPath(path)
class Template(QWidget):
def __init__(self):
url = os.path.join(DIR_PATH, "my-font.ttf")
font_id = QFontDatabase.addApplicationFont(url)
if font_id == -1:
print('not font')
font = QFont("my-font",18)
super().__init__()
grid = QGridLayout(self)
grid.addWidget(QLabel('<div></div>'), 0, 0)
grid.addWidget(ThinLabel('<div></div>'), 1, 0)
self.setStyleSheet('''
QLabel {
font-size: 80pt;
font-family: my-font;
}''')
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = Template()
gui.show()
sys.exit(app.exec_())
And I need to make the character a little thinner But the text does not translate into a symbol inside the paintEvent
How do I translate text into a symbol?
I have tried
connect the font inside ThinLabel(QLabel)
create another label inside ThinLabel(QLabel) but setBrush does not accept self.label.palette().text()
来源:https://stackoverflow.com/questions/61005314/why-the-symbol-xe202-does-not-translate-to-inside-the-paintevent