3

I am building an app and need to add a custom font of my desire in it, I have tried multiple ways to get that font loaded with a relative path but have failed with the error:

qt.qpa.fonts: Populating font family aliases took 192 ms. Replace uses of missing font family "ITCAvantGardeBk" with one that exists to avoid this cost. 

I am fed up of this error, suggest me some working ways of adding custom fonts using relative path and not absolute

These are my last attempts for getting the fonts loaded in my PyQt6 application using relative path

main.py

QFontDatabase.addApplicationFont("rsrc/ITCAvantGardeBk.ttf")
with open('styles.qss', 'r') as stlf:
    style = stlf.read()
    app.setStyleSheet(style)
# stylesheet = open('rsrc/vault8_styles.qss').read()
# app.setStyleSheet(stylesheet)
sys.exit(app.exec())

styles.qss


/* @font-face {
    font-family: ITC Avant Garde;
    src: url(rsrc/ITCAvantGardeBk.ttf);
} */

QLabel {
    font-family: 'ITCAvantGardeBk';
}

I have tried CodersLegacy's turn for adding fonts, this is how it went:

from PyQt6.QtWidgets import QApplication, QWidget, QLabel
from PyQt6.QtGui import QFont, QFontDatabase
import sys
 
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(600, 300)
        self.setWindowTitle("CodersLegacy")
        self.setContentsMargins(20, 20, 20, 20)
 
        id = QFontDatabase.addApplicationFont("rsrc/ITCAvantGardeStd.ttf")
        if id < 0: print("Error")
        else: print("Success", id)
 
        families = QFontDatabase.applicationFontFamilies(id)
        print(families[0])
 
        label = QLabel("Hello World", self)
        label.setFont(QFont(families[0], 80))
        label.move(50, 100)
 
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

Output:

ray@Ray project % /usr/local/bin/python3 "/Volumes/Alpha/Workspaces/Python/Projects/project-beta/project/test.py"
Error
Traceback (most recent call last):
  File "/Volumes/Alpha/Workspaces/Python/Projects/project-beta/project/test.py", line 24, in <module>
    window = Window()
  File "/Volumes/Alpha/Workspaces/Python/Projects/project-beta/project/test.py", line 17, in __init__
    print(families[0])
IndexError: list index out of range

However changing font path from relative to absolute spits out

Success 0
ITC Avant Garde Gothic Std

Now I want this, but with relative path-

musicamante
  • 41,230
  • 6
  • 33
  • 58
Ray
  • 31
  • 3
  • 1
    Relative paths are usually not relative to the file, but to the working directory, which is the path from which the python interpreter or script is run. You're probably running the script using a path or your IDE is. Use `os.path` or the pathlib functions to retrieve the absolute path from a relative one. – musicamante Oct 28 '22 at 16:16
  • @musicamante Would that affect the final build if I want to run in a different system (or operating system)? Thanks for the suggestion! – Ray Oct 28 '22 at 17:11
  • Not at all, on the contrary, using dynamic path construction ensures that it will always be the correct one, also respecting the systems directory separators. Be aware that if you plan to use pyinstaller (or similar tools), you need to use a custom function to get the correct path when files are bundled in a single binary. – musicamante Oct 28 '22 at 17:19
  • 1
    Really wonderful suggestion using os.path, I got it fixed now and it's working flawlessly! Thanks a lot @musicamante! – Ray Oct 29 '22 at 09:11
  • You're welcome, but please don't edit the question with the solution, there is the *answer field* for that. – musicamante Oct 29 '22 at 17:01
  • Ohh, didn't notice that, I am new here, thanks for the tip! – Ray Oct 31 '22 at 12:48

0 Answers0