Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFontDialog doesn't return (sometimes) QFont with correct styleName
Forum Updated to NodeBB v4.3 + New Features

QFontDialog doesn't return (sometimes) QFont with correct styleName

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 154 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chrisRonov
    wrote on last edited by
    #1

    Hello,

    I have problem with QFontDialog dosen't return QFont object with correct styleName for some specific fonts. It returns empty styleName. Fonts which doesn't work have Normal instead of Regular style. On my system (Win 64) I have 'Arial', which works, and also font 'Bahnschrift' which doesn't return styleName.

    It can be reproduced on the Linux as well, but with different font.
    The problem can be easy reproduced with a code bellow.
    Select 'Arial' - font styleName is ok - exactly what user selects
    Select 'Bahnschrift' - font styleName is empty - independet what user selects

    #include <QApplication>
    #include <QDebug>
    #include <QFontDialog>
    
    class QFontDialogTester : public QWidget
    {
    public:
      void onFont()
      {
        bool ok;
        QFont font = QFontDialog::getFont(
                        &ok,
                        QFont( "Arial", 18 ),
                        this,
                        tr("Pick a font") );
        if( ok )
        {
          qDebug() << "font           : " << font;
          qDebug() << "font weight    : " << font.weight();
          qDebug() << "font family    : " << font.family();
          qDebug() << "font style     : " << font.style();  //  StyleNormal = 0, StyleItalic = 1, StyleOblique = 2
          qDebug() << "font styleName : " << font.styleName();
          qDebug() << "font pointSize : " << font.pointSize();
    
        }
      }
    
    };
    
    int main( int argc, char **argv )
    {
      QApplication app( argc, argv );
      QFontDialogTester font_test;
      font_test.onFont();
      return 0;
    }
    
    

    Investigating Qt code I have found, that QFontDatabase::font( ....) method doesn't return the QFont object with correct styleName, despite the style name is specified and such font exists on the system.

    I have also found a solution. I added following code

        // Patch - prevent returning an empty StyleName
        // if styleName still empty, try to find it in the allStyles
        // inspired by QFontDatabase::styles(..)
        if (fnt.styleName().isEmpty())
        {
            for (int i = 0; i < allStyles.count; i++) {
                QString listedStyle = allStyles.styles[i]->styleName.isEmpty()
                                 ? styleStringHelper(allStyles.styles[i]->key.weight,
                                                     (QFont::Style)allStyles.styles[i]->key.style)
                                 : allStyles.styles[i]->styleName;
                if (listedStyle == style) {
                    fnt.setStyleName(style);
                }
            }
        }
    

    at the end of the QFontDatabase::font( ....) method, just before the last return statement.

    It works, however I am not confident with the "Fonts topic", and I don't know if this is the right way how to correct this issue.
    I would like to prevent my own changes in Qt code.
    If it is a bug, it should be corrected in the official code.

    Please let me know what to do - if it is a bug (how to post a bug) and what I have to check/change if it is not a bug?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved