How do I set my font?

My code

import sys
from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(1038, 722)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.label = QLabel(self.centralwidget)
        self.label.setObjectName(u"label")
        self.label.setGeometry(QRect(10, 10, 191, 121))


        font_id = QFontDatabase.addApplicationFont(":/my_font.ttf")
        font = QFont("my_font", 12)
        font.setFamily(u"my_font")
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)

        self.label.setText('&#e435')



        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 1038, 21))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))

StyleSheet = """
#centralwidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
background:#000;
color:#fff;
}
#centralwidget QWidget,
#centralwidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget,
#centralwidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget QWidget{
background:#fff;
color:#000;
}
"""

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(StyleSheet)
    window = MainWindow()
    window.show()
sys.exit(app.exec_())

I need the program to show me an icon instead of e435, but the problem is that I do not know how to connect the font. addApplicationFont () doesn't tell me if it found the font on this path or not. So I don't even know what the problem might be. Does anyone know the solution to this problem?

Author: or latym, 2020-03-17

1 answers

AddApplicationFont() doesn't tell me if it found a font along this path or not.

According to the description of the int QFontDatabase::addApplicationFont(const QString &fileName) method, it will return -1 if the font file was not found:

The function returns -1 if the font could not be loaded.

Therefore, add a check, for example:

font_id = QFontDatabase.addApplicationFont(":/my_font.ttf")
if font_id == -1:
    QMessageBox.warning(self, "Внимание", "Пользовательский шрифт не был найден!")
...

And the colon (:/) in the font path ":/my_font.ttf" corresponds to the path in the resource file Qt (format .qrc), if you do not use qrc, then specify the standard full or relative path to the file

 1
Author: gil9red, 2020-03-17 06:51:25