laserpants / qt-material-widgets

:art: Qt widgets-based implementation of the Material Design specification.
BSD 3-Clause "New" or "Revised" License
2.94k stars 660 forks source link

problems with font (introducing ListItem) #24

Closed fperillo closed 6 years ago

fperillo commented 6 years ago

In my fork I pushed a first try at ListItem. It is a test.. and both worked and not worked... one-line_listitem

As you can see, the font is "strange"... I think that Qt is stretching it a bit...

I did some more tests and noticed that I don't have Roboto font installed on my workstation. And that the library doesn't load them with ::addApplicationFont. Also loading the font, I still have problems, the font is not used. If I ask for an installed font, it is used without problem. Also trying to load another external font I get the default font....

Actually, wherever you have to use this code snippet:

   QFont font(db.font("Roboto", "Regular", 16));
   font.setPointSize(16); // it should not be necessary

means that font object has no size 16 and you have to force it....

I don't know what other test to do...

Do you have the font installed?

laserpants commented 6 years ago

The font files should be bundled with the library as resources.

See components/resources.qrc:

<RCC>
    <qresource prefix="/fonts">
        <file>../fonts/Roboto/Roboto-Black.ttf</file>
        <file>../fonts/Roboto/Roboto-Bold.ttf</file>
        <file>../fonts/Roboto/Roboto-Medium.ttf</file>
        <file>../fonts/Roboto/Roboto-Regular.ttf</file>
        <file>../fonts/Roboto/Roboto-Light.ttf</file>
        <file>../fonts/Roboto/Roboto-Thin.ttf</file>
    </qresource>
...

These are then loaded by the style class (QtMaterialStyle). See qtmaterialstyle.cpp, line 23:

    QFontDatabase::addApplicationFont(":/material/fonts/Roboto/Roboto-Regular.ttf");
    QFontDatabase::addApplicationFont(":/material/fonts/Roboto/Roboto-Medium.ttf");
    QFontDatabase::addApplicationFont(":/material/fonts/Roboto/Roboto-Bold.ttf");
laserpants commented 6 years ago

I don't have the font installed on my system, no.

fperillo commented 6 years ago

I used the unix switch for grep on a windows workstation... no dir recursion... sorry about this.

This evening I will try on my home notebook, and will try this code snippet to see what happens with the fonts: https://bugreports.qt.io/browse/QTBUG-61520

I still think that setPointSize call should not be necessary it the correct font is returned from the FontDatabase.

fperillo commented 6 years ago

I confirm there is a problem with the font loading. which-is-roboto Which one is Roboto?

I think the problem is in the resource file... I'm not even sure you are using roboto font due to a mismatch in the path name... QFontDatabase::addApplicationFont(":/material/fonts/Roboto/Roboto-Regular.ttf");

See the changes in the respource file: https://github.com/fperillo/qt-material-widgets/commit/1bfb9990054121d8720df170d364282c958a14da and where I add the fonts using the aliases

laserpants commented 6 years ago

What if you put the fonts in the same directory as the icons? I mean, just copy the whole fonts directory into components/ and then change the path in the resource file from ../fonts/Roboto to fonts/Roboto.

laserpants commented 6 years ago

The resource path is not a one-to-one mapping to the file system path, because of how the resource system works in Qt. See http://doc.qt.io/qt-5/resources.html. (The IDE complains if I try to use an incorrect resource path.)

laserpants commented 6 years ago

I'll try on a different machine also when I get the chance, to see if I can figure out what the problem is.

fperillo commented 6 years ago

An alias should be used in the resource file, in this way:

../fonts/Roboto/Roboto-Medium.ttf

then load in this way: QFontDatabase::addApplicationFont( ":/fonts/rmedium" );

This is an official way to load resources via aliases.

I then created font via: QFont font("Roboto", 14, iFontWeight ) where f.e. iFontWeight = QFont::Bold

laserpants commented 6 years ago

Ok, I think I understand now. Does https://github.com/laserpants/qt-material-widgets/commit/2ee5d6dd5de94e8bade310330a6f8e651edbb60e work or?

fperillo commented 6 years ago

It works now :-)

Thank you