QFontDatabase/QFont question

John Sturtz john at sturtz.org
Sat Jan 11 15:08:28 GMT 2025


😊  Thanks again.  Boy, I've got a lot to learn ...

I saw those other .ttf files (actually, in the distribution I got, 
they're in a subdirectory called static, which really ought to have 
tipped me off), but I had overlooked them.

Is there a way to tell from Qt whether a font is variable or static?  
Windows Font Settings seems to know (it displays 'Variable font' for 
some fonts and not for others), so I guess it must be a contained in the 
font file?

------ Original Message ------
>From "Charles" <peacech at gmail.com>
To "John Sturtz" <john at sturtz.org>
Cc "Maurizio Berti" <maurizio.berti at gmail.com>; 
pyqt at riverbankcomputing.com
Date 1/10/2025 7:45:38 PM
Subject Re: Re[4]: QFontDatabase/QFont question

> > I thought the font was inherently variable?
>
>If you download the font from Google Fonts, there are 2 ttf files with 
>"variable" in the name, these are the variable fonts. There are also a 
>bunch of ttf files in a "ttf" directory, these are the static fonts. It 
>is not about how you access the font, it is about which ttf files you 
>install.
>
> > QFontDatabase offers no italic style for Bahnschrift Condensed.  If I 
>create with QFont(), it is initially condensed.  But if I call 
>.setItalic(True), it no longer is condensed; it's just plain 
>Bahnschrift.
>
>You need to set both the style name and the stretch. The font name and 
>style flags do the font matching while the style name limits the 
>matched fonts. Since there are no matching fonts with the style flags, 
>obliquefication uses whatever font matched first. If you set the style 
>name, obliqueficiation is limited to the font with that style.
>
>f = QFont('Bahnschrift', 24)
>f.setStyleName('Condensed')
>f.setStretch(QFont.Stretch.Condensed)
>f.setItalic(True)
>
>On Sat, Jan 11, 2025 at 8:01 AM John Sturtz <john at sturtz.org> wrote:
>>------ Original Message ------
>>From "Charles" <peacech at gmail.com>
>>To "John Sturtz" <john at sturtz.org>
>>Cc "Maurizio Berti" <maurizio.berti at gmail.com>; 
>>pyqt at riverbankcomputing.com
>>Date 1/9/2025 10:55:57 PM
>>Subject Re: Re[2]: QFontDatabase/QFont question
>>
>>> > you're getting a named style from the variable font file
>>>
>>>You didn't mention that you are using the variable font. I didn't use 
>>>the variable font instead I use the static fonts. I also got the thin 
>>>oblique font with the variable fonts.
>>I thought the font was inherently variable?  Or is it a matter of how 
>>you access it?  Use QFontDatabase() (accessing by style name), and you 
>>are using it as a variable font; use QFont + .setWeight() and 
>>.setItalic() methods, and you are using it as static (= 'simulated' = 
>>'synthesized').  Do I understand that correctly?
>>
>>>
>>>These codes works for me:
>>>
>>>1. f = QFont('Exo', 24, 700, True) # use QFont specifying weight and 
>>>italic
>>>2. f = QFont('Exo', 24); f.setWeight(700); f.setItalic(True)
>>>3. f = QFontDatabase('Exo', 'Bold', 24); f.setStyleName('Bold 
>>>Italic')
>>Yes, these all work for me as well [the last needs to be 
>>QFontDatabase.font('Exo', 'Bold', 24)]
>>
>>>Note that italic is part of the style name. If you call 
>>>setItalic(True) there is a mismatch between the style flags and the 
>>>style name. So if you use QFontDatabase and style name always use 
>>>setStyleName to change the style, otherwise use QFont and setWeight 
>>>and setItalic.
>>So it seems the moral is not to intermingle QFont (static) and 
>>QFontDatabase (variable) methods.
>>
>>But this case still troubles me:
>>
>>I see the problem with other fonts as well -- Bahnschrift, for 
>>example, which comes with Windows.  If I create a QFont object with 
>>QFontDatabase.font():
>>
>>         f = QFontDatabase.font('Bahnschrift', 'Condensed', 24)
>>
>>and then apply .setItalic(True) to it, it's italic, but no longer 
>>Condensed.
>>
>>And actually, the same thing happens if I create the font with QFont() 
>>directly:
>>
>>         f = QFont(
>>             'Bahnschrift Condensed',
>>             pointSize=24
>>         )
>>
>>Here also, the font is fine initially, but after .setItalic(True) it 
>>is no longer Condensed.
>>
>>QFontDatabase offers no italic style for Bahnschrift Condensed.  If I 
>>create with QFont(), it is initially condensed.  But if I call 
>>.setItalic(True), it no longer is condensed; it's just plain 
>>Bahnschrift.
>>
>>I'd think this must be a bug?
>>
>>/John
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20250111/22014823/attachment.htm>


More information about the PyQt mailing list