QFontDialog::getFont() doesn't use correct font style
-
Hi all,
I want to use a QFontDialog for setting a font.
When I use code like this:
bool ok; QFont font; font.setFamily("Norasi"); font.setWeight(50); font.setStyle(QFont::StyleNormal); QFontDialog::getFont(&ok, font, this);
The dialog shows this:
It always uses the first font style in the list instead of the style (normal) and weight (regular) of the QFont parameter.
I was testing under:Platform:
linux
Operating System:KDE neon User Edition 5.7
Build architecture:x86_64
Current architecture:x86_64
Qt Version (build):5.7.0
Qt Version (runtime):5.7.0
(The problem was initially reported by a user of QOwnNotes at https://github.com/pbek/QOwnNotes/issues/239.)
Am I doing something wrong?
Thanks a lot for your help! -
@Ratzz said:
QFont serifFont("Times", 10, QFont::Bold)
QFont font("Norasi", 10, 50, false)
doesn't work neither.
My real use-case is setting the font with the QFontDialog, storing it to the settings, loading it from the settings and showing it with the QFontDialog again. This doesn't work neither. -
But there is only
Normal
at http://doc.qt.io/qt-5/qfont.html#Style-enum. :(
And the real thing I am doing in https://github.com/pbek/QOwnNotes/blob/develop/src/dialogs/settingsdialog.cpp is saving a selected font to the setting and loading it again next time the settings dialog is opened. When I then open the font dialog with that font the style is wrong... -
I made a new topic with a better example: https://forum.qt.io/topic/69348/qfontdialog-getfont-doesn-t-use-correct-font-style-when-font-of-the-font-parameter-use-tostring-and-fromstring
-
@kshegunov, could you please open https://forum.qt.io/topic/69348/qfontdialog-getfont-doesn-t-use-correct-font-style-when-font-of-the-font-parameter-use-tostring-and-fromstring again, since it has the more valid test to reproduce the problem. We can mark this topic as duplicate instead, if that's ok with you. It wasn't my intention to cause any harm, the two cases could be related and I wasn't sure if I it's good to post the whole topic text as reply here.
-
On 2nd glance, maybe it's really better to reply here.
So here comes by better example:QFontDialog::getFont() doesn't use the correct font style when the font of the font parameter uses toString() and fromString().
It always uses the first font style in the list instead of the style (normal) and weight (regular) of the QFont parameter.
I made an example:bool ok; // select a font where `Regular` is not the first item in the `Font // style` selector (e.g. `Noto Sans`) and select `Regular` QFont font = QFontDialog::getFont(&ok); if (ok) { // looks like `QFont( "Noto Sans,11,-1,5,50,0,0,0,0,0" )` qDebug() << "'font': " << font; QFont font2; // I originally store the font to the settings and load it again, but // that's not even needed to reproduce this problem font2.fromString(font.toString()); // still looks like `QFont( "Noto Sans,11,-1,5,50,0,0,0,0,0" )` qDebug() << "'font2': " << font2; // now `Bold Italic` is selected, because it's the first item QFont font3 = QFontDialog::getFont(&ok, font2, this); if (ok) { // would now look like `QFont( "Noto Sans,11,-1,5,75,1,0,0,0,0" )` qDebug() << "'font3': " << font3; } }
I was testing under:
Platform:
linux
Operating System:KDE neon User Edition 5.7
Build architecture:x86_64
Current architecture:x86_64
Qt Version (build):5.7.0
Qt Version (runtime):5.7.0
(The problem was initially reported by a user of QOwnNotes at https://github.com/pbek/QOwnNotes/issues/239.)
Am I doing something wrong?
Thanks a lot for your help! -
@pbe said:
On 2nd glance, maybe it's really better to reply here.
It is, indeed, for several reasons.
- The whole problem history is at a glance
- The people who responded will get a notification there's activity on the topic.
- The additional information may attract more or better answers
and so on.
Kind regards.
-
@pbe
Hi,
There's a bug - QTBUG-52741 that appears to be what you're describing. I also run your example through the debugger after I was able to reproduce (Qt 5.7) and the problem is indeed in the dialog, here.The dialog matches the family only, and when it calls
setCurrentItem
it selects the very first item in the model (which has the default style), which in turn will overwrite the member that holds the style and you get what you described. If you use the font object directly you won't get the bug, i.e.:QFont font = QFontDialog::getFont(&ok); if (ok) { // ... QFont font2 = QFontDialog::getFont(&ok, font, this); //< Works correctly, no bug // ... }
Kind regards.
-
@kshegunov, I already found that bug and was wondering if that's what's affecting QOwnNotes. Thank you for making that clear!
Yes, you are right. If you use the font directly it works, but it doesn't work with
font2.fromString(font.toString());
that is needed to store and load it to/from the settings. Do you have any ideas to get around this? -
@pbe said:
Do you have any ideas to get around this?
Sadly, no. The bug seems inherent to the
QFontDialog
class when theQFont
's internal identifier isn't set (i.e. when the dialog needs to go and query about the font by its attributes). If I get some more time in the coming days, I'll try to run it again and hopefully will have something better.Kind regards.
-
@kshegunov, thank you very much! You are my hero!
-
@pbe
https://codereview.qt-project.org/#/c/165958/We'll see if it gets approved.