Skip to content

Font

Qt Base Class: QFont

Signature: QFont(self) -> None QFont(self, families: Sequence[str], pointSize: int = -1, weight: int = -1, italic: bool = False) -> None QFont(self, family: str, pointSize: int = -1, weight: int = -1, italic: bool = False) -> None QFont(self, font: Union[PySide6.QtGui.QFont, str, Sequence[str]]) -> None QFont(self, font: Union[PySide6.QtGui.QFont, str, Sequence[str]], pd: PySide6.QtGui.QPaintDevice) -> None

Base classes

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

⋔ Inheritance diagram

graph TD
  1473245607040["gui.Font"]
  1473300151664["QtGui.QFont"]
  1473291690208["Shiboken.Object"]
  140713234304496["builtins.object"]
  1473300151664 --> 1473245607040
  1473291690208 --> 1473300151664
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: QFont

Specifies a query for a font used for drawing text.

Source code in prettyqt\gui\font.py
class Font(gui.QFont):
    """Specifies a query for a font used for drawing text."""

    def __repr__(self):
        return get_repr(
            self, self.family(), self.pointSize(), self.weight(), self.italic()
        )

    def __getstate__(self):
        return dict(
            family=self.family(),
            pointsize=self.pointSize(),
            weight=self.weight(),
            italic=self.italic(),
        )

    def __setstate__(self, state):
        self.setFamily(state["family"])
        if state["pointsize"] > -1:
            self.setPointSize(state["pointsize"])
        self.setWeight(state["weight"])
        self.setItalic(state["italic"])

    @property
    def _family(self) -> str:
        return self.family()

    __match_args__ = ("_family",)

    def __reduce__(self):
        return type(self), (), self.__getstate__()

    def serialize(self) -> dict[str, Any]:
        return self.__getstate__()

    @property
    def metrics(self) -> gui.FontMetrics:
        return gui.FontMetrics(self)

    def set_size(self, size: int):
        self.setPointSize(size)

    @classmethod
    def mono(cls, size=8, as_qt: bool = False) -> Self:
        match sys.platform:
            case "win32":
                font = "Consolas"
            case "darwin":
                font = "Menlo"
            case _:
                font = "Monospace"
        if as_qt:
            return gui.QFont(font)
        else:
            return cls(font, size)
        # font.setStyleHint()

    def scaled(self, factor: float) -> Self:
        scaled = type(self)(self)
        if self.pointSizeF() != -1:
            scaled.setPointSizeF(self.pointSizeF() * factor)
        elif self.pixelSize() != -1:
            scaled.setPixelSize(int(self.pixelSize() * factor))
        return scaled

    def set_style_hint(self, hint: StyleHintStr | gui.QFont.StyleHint):
        """Set the style hint.

        Args:
            hint: style hint
        """
        self.setStyleHint(STYLE_HINTS.get_enum_value(hint))

    def set_weight(self, weight: WeightStr | gui.QFont.Weight):
        """Set the font weight.

        Args:
            weight: font weight
        """
        self.setWeight(WEIGHT.get_enum_value(weight))

    def get_weight(self) -> WeightStr:
        """Get current font weight.

        Returns:
            current font weight
        """
        return WEIGHT.inverse[self.weight()]

    def set_capitalization(
        self, capitalization: CapitalizationStr | gui.Font.Capitalization
    ):
        """Set the font capitalization.

        Args:
            capitalization: font capitalization
        """
        self.setCapitalization(CAPITALIZATION.get_enum_value(capitalization))

    def get_capitalization(self) -> CapitalizationStr:
        """Get current font capitalization.

        Returns:
            current font capitalization
        """
        return CAPITALIZATION.inverse[self.capitalization()]

    def set_hinting_preference(
        self, preference: HintingPreferenceStr | gui.Font.HintingPreference
    ):
        """Set the hinting preference.

        Args:
            preference: hinting preference
        """
        self.setHintingPreference(HINTING_PREFERENCE.get_enum_value(preference))

    def get_hinting_preference(self) -> HintingPreferenceStr:
        """Get current hinting preference.

        Returns:
            current hinting preference
        """
        return HINTING_PREFERENCE.inverse[self.hintingPreference()]

    def set_letter_spacing(
        self, typ: SpacingTypeStr | gui.Font.SpacingType, spacing: float
    ):
        """Set the letter spacing.

        Args:
            typ: letter spacing type
            spacing: spacing
        """
        self.setLetterSpacing(SPACING_TYPE.get_enum_value(typ), spacing)

    def get_letter_spacing_type(self) -> SpacingTypeStr:
        """Get current letter spacing type.

        Returns:
            current letter spacing type
        """
        return SPACING_TYPE.inverse[self.letterSpacingType()]

    def set_style(self, style: StyleStr | gui.Font.Style):
        """Set the font style.

        Args:
            style: font style
        """
        self.setStyle(STYLE.get_enum_value(style))

    def get_style(self) -> StyleStr:
        """Get current font style.

        Returns:
            current font style
        """
        return STYLE.inverse[self.style()]

    def set_family(self, family: str, fallback: str | None = None):
        """Set the font family.

        Args:
            family: font family
            fallback: fallback font family
        """
        self.setFamily(family)
        font_info = gui.FontInfo(self)
        if fallback is not None and font_info.family() != family:
            self.setFamily(fallback)

get_capitalization() -> CapitalizationStr

Get current font capitalization.

Source code in prettyqt\gui\font.py
def get_capitalization(self) -> CapitalizationStr:
    """Get current font capitalization.

    Returns:
        current font capitalization
    """
    return CAPITALIZATION.inverse[self.capitalization()]

get_hinting_preference() -> HintingPreferenceStr

Get current hinting preference.

Source code in prettyqt\gui\font.py
def get_hinting_preference(self) -> HintingPreferenceStr:
    """Get current hinting preference.

    Returns:
        current hinting preference
    """
    return HINTING_PREFERENCE.inverse[self.hintingPreference()]

get_letter_spacing_type() -> SpacingTypeStr

Get current letter spacing type.

Source code in prettyqt\gui\font.py
def get_letter_spacing_type(self) -> SpacingTypeStr:
    """Get current letter spacing type.

    Returns:
        current letter spacing type
    """
    return SPACING_TYPE.inverse[self.letterSpacingType()]

get_style() -> StyleStr

Get current font style.

Source code in prettyqt\gui\font.py
def get_style(self) -> StyleStr:
    """Get current font style.

    Returns:
        current font style
    """
    return STYLE.inverse[self.style()]

get_weight() -> WeightStr

Get current font weight.

Source code in prettyqt\gui\font.py
def get_weight(self) -> WeightStr:
    """Get current font weight.

    Returns:
        current font weight
    """
    return WEIGHT.inverse[self.weight()]

set_capitalization(capitalization: CapitalizationStr | gui.Font.Capitalization)

Set the font capitalization.

Parameters:

Name Type Description Default
capitalization CapitalizationStr | Capitalization

font capitalization

required
Source code in prettyqt\gui\font.py
def set_capitalization(
    self, capitalization: CapitalizationStr | gui.Font.Capitalization
):
    """Set the font capitalization.

    Args:
        capitalization: font capitalization
    """
    self.setCapitalization(CAPITALIZATION.get_enum_value(capitalization))

set_family(family: str, fallback: str | None = None)

Set the font family.

Parameters:

Name Type Description Default
family str

font family

required
fallback str | None

fallback font family

None
Source code in prettyqt\gui\font.py
def set_family(self, family: str, fallback: str | None = None):
    """Set the font family.

    Args:
        family: font family
        fallback: fallback font family
    """
    self.setFamily(family)
    font_info = gui.FontInfo(self)
    if fallback is not None and font_info.family() != family:
        self.setFamily(fallback)

set_hinting_preference(preference: HintingPreferenceStr | gui.Font.HintingPreference)

Set the hinting preference.

Parameters:

Name Type Description Default
preference HintingPreferenceStr | HintingPreference

hinting preference

required
Source code in prettyqt\gui\font.py
def set_hinting_preference(
    self, preference: HintingPreferenceStr | gui.Font.HintingPreference
):
    """Set the hinting preference.

    Args:
        preference: hinting preference
    """
    self.setHintingPreference(HINTING_PREFERENCE.get_enum_value(preference))

set_letter_spacing(typ: SpacingTypeStr | gui.Font.SpacingType, spacing: float)

Set the letter spacing.

Parameters:

Name Type Description Default
typ SpacingTypeStr | SpacingType

letter spacing type

required
spacing float

spacing

required
Source code in prettyqt\gui\font.py
def set_letter_spacing(
    self, typ: SpacingTypeStr | gui.Font.SpacingType, spacing: float
):
    """Set the letter spacing.

    Args:
        typ: letter spacing type
        spacing: spacing
    """
    self.setLetterSpacing(SPACING_TYPE.get_enum_value(typ), spacing)

set_style(style: StyleStr | gui.Font.Style)

Set the font style.

Parameters:

Name Type Description Default
style StyleStr | Style

font style

required
Source code in prettyqt\gui\font.py
def set_style(self, style: StyleStr | gui.Font.Style):
    """Set the font style.

    Args:
        style: font style
    """
    self.setStyle(STYLE.get_enum_value(style))

set_style_hint(hint: StyleHintStr | gui.QFont.StyleHint)

Set the style hint.

Parameters:

Name Type Description Default
hint StyleHintStr | StyleHint

style hint

required
Source code in prettyqt\gui\font.py
def set_style_hint(self, hint: StyleHintStr | gui.QFont.StyleHint):
    """Set the style hint.

    Args:
        hint: style hint
    """
    self.setStyleHint(STYLE_HINTS.get_enum_value(hint))

set_weight(weight: WeightStr | gui.QFont.Weight)

Set the font weight.

Parameters:

Name Type Description Default
weight WeightStr | Weight

font weight

required
Source code in prettyqt\gui\font.py
def set_weight(self, weight: WeightStr | gui.QFont.Weight):
    """Set the font weight.

    Args:
        weight: font weight
    """
    self.setWeight(WEIGHT.get_enum_value(weight))