I have a font that has several font weights within one family (extra light, light, regular, semibold, bold, black). For this example, I'm using the free Google Webfont Source Sans Pro, but I've had the same issue with any font that has multiple weights (such as Avenir Next, a system font in Mac OS 10.8). In a Qt stylesheet, if I specify:
QLabel
{
font-family: Source Sans Pro;
font-size: 24px;
}
I get the "Regular" style of the font. If I specify:
QLabel
{
font-family: Source Sans Pro;
font-weight: bold;
font-size: 24px;
}
I get the bold version of the font. I can't figure out any combination that will give me anything other than regular or Bold. I tried putting the weight directly in the font-family (e.g., font-family: Source Sans Pro Light; ), I tried using Light, Semibold, etc. in the font-weight: specifier, I also tried using the numeric equivalents (Qt documentation says they're supposed to be values from 0 to 99 rather than the CSS x00 numbering). Nothing works.
EDIT: I did just try writing a quick program to sub in numbers between 0 and 999 for the font-weight property. Interestingly, I was able to get "light" if I put in anything between 0 and 399. 400 through 407 gave me "Regular," 408 through 599 gave me light again, then 600 or higher gave me semibold. This seems really odd, because now using numbers I can at least access "light" and "semibold." I still can't find any way to get the "Extra Light" or the "Black" weights.
My only question is, is this just impossible without resorting to custom code? I can get the exact font I want by creating a QFont as follows:
QFontDatabase db;
QFont = db.font("Source Sans Pro", "Semibold", 24 );
But obviously this means I can't use stylesheets (or worse, I have to write my own stylesheet parser or wrap the existing Qt one or something). This seems a little hard to believe that this isn't possible with stylesheets. Are multi-weight fonts just poorly supported in Qt?
The font property may be specified as either a single keyword, which will select a system font, or as a shorthand for various font-related properties. If font is specified as a system keyword, it must be one of: caption , icon , menu , message-box , small-caption , status-bar .
Default Application Font QT has a default font. That default font is different on different platforms. On Windows it is "Arial" (a sanserif style) but on Linux it is a serifed style similar to Times Roman. If your application is going to run on different platforms you should define "Lato" as the default font.
Finally trolled through the Qt source and I think I found the answer. In short, no, its not possible. Or at least, it depends on the font, and perhaps even the platform (I'm working in Mac OS X with Qt 4.8.4).
QFonts have both font "weights" (set by QFont::setWeight()), which is a number from 0-99. They also have a "Style Name" which is set by QFont::setStyleName(). You can set the weight using font-weight in a stylesheet, but you cannot set the styleName (I confirmed by looking at the source of qcssparser). There is a font-style parameter, but that can only be used to set normal, italic or oblique... that is, it calls QFont::setStyle(), not setStyleName().
At least in my case, the only way to access certain weights of the font is by the "Style Name," giving it a specific string like "Extra Light." Apparently, Qt is doing some internal (perhaps platform-specific) magic to convert between the 0-99 weight number and a style name, and it doesn't seem to do it well. I got too bogged down in the Qt sources to really figure out exactly where this mapping was happening.
To add to the weirdness, QFont::setWeight takes a number between 0-99 (as I previously stated), but the font-weight property in the stylesheet takes a number between 100 and 800, then divides that by 8 and calls setWeight. I suppose this is to more closely match the CSS weight numbering system.
Now I just have to figure out what kind of hack I'm going to do to get around this. I can easily add something like font-style-name to the qcssparser, but I'd rather not patch the Qt sources, so I'll probably do something at the application level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With