Skip to content

FontDatabase

Qt Base Class: QFontDatabase

Signature: QFontDatabase(self) -> None QFontDatabase(self, QFontDatabase: PySide6.QtGui.QFontDatabase) -> None

Base classes

Name Children Inherits
QFontDatabase
PySide6.QtGui
QFontDatabase(self) -> None

⋔ Inheritance diagram

graph TD
  1473245610944["gui.FontDatabase"]
  1473300149712["QtGui.QFontDatabase"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473300149712 --> 1473245610944
  1473291690208 --> 1473300149712
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: QFontDatabase

Information about the fonts available in the underlying window system.

Source code in prettyqt\gui\fontdatabase.py
class FontDatabase(QtGui.QFontDatabase):
    """Information about the fonts available in the underlying window system."""

    font_paths: dict[str, int] = {}

    @classmethod
    def add_fonts_from_folder(cls, path: datatypes.PathType):
        path = pathlib.Path(path)
        for p in path.iterdir():
            if p.suffix.lower() in [".ttf", ".otf"]:
                logger.debug(f"adding font {p!r} to database.")
                cls.addApplicationFont(str(p))

    @classmethod
    def add_font(cls, path: datatypes.PathType, ttf_hash: str | None = None) -> int:
        path = pathlib.Path(path)
        font_id = cls.addApplicationFont(str(path))
        if not cls.applicationFontFamilies(font_id):
            raise RuntimeError(
                f"Font {path!r} appears to be empty. "
                "If you are on Windows 10, please read "
                "https://support.microsoft.com/"
                "en-us/kb/3053676"
            )
        if ttf_hash is not None:
            content = path.read_bytes()
            if hashlib.md5(content).hexdigest() != ttf_hash:
                raise OSError(f"Font is corrupt at: {path!r}")
        cls.font_paths[str(path)] = font_id
        return font_id

    @classmethod
    def remove_font(cls, font: datatypes.PathType | int):
        font_id = font if isinstance(font, int) else cls.font_paths[str(font)]
        cls.removeApplicationFont(font_id)

    @classmethod
    def get_system_font(cls, font_type: SystemFontStr | QtGui.QFontDatabase.SystemFont):
        return cls.systemFont(SYSTEM_FONT[font_type])