Reputation: 31
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-
Upvotes: 3
Views: 958