Why the symbol  does not translate to ✰ inside the paintEvent?

℡╲_俬逩灬. 提交于 2021-02-05 09:39:20

问题


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>&#xe202;</div>'), 0, 0)
        grid.addWidget(ThinLabel('<div>&#xe202;</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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!