7
votes

I'm trying to use QML's built-in FontLoader element to load a custom font for our application without success.

I've tried using both OTF and TTF fonts with identical results.

The fonts are in the same directory as the project files. There is only one QML, the main one where this FontLoader lives.

This is what it should look like:

sample screenshot

Here is my code:

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360

    FontLoader {
        id: cFontLoader
        source: "./fontlol.ttf"
    }

    Text {
        id: testText
        anchors.centerIn: parent
        text: "Hello Fonts!"
        font.family: cFontLoader.name
        font.pointSize: 32
    }
}
4
Alright, so it seems that using single-style such as Bebas Neue works ok. The issue is we need to use Helvetica Neue LT Pro which has various sub-styles: puu.sh/27fq9/e42b7c3004 How do i specify the one i want? - Andrew
At printscreent i see .otf file - progpow
I got this solved a long time ago but forgot to post back. For some reason QML just doesn't like some font files. They'll work fine in other apps, on the web, etc, but will not render in at all in QML. I was never able to figure out why but i did manage to find a font pack that worked. Was just trial and error. - Andrew

4 Answers

6
votes

I've had headaches with Qt/QML's font handling. Fonts with "various sub styles" seem to be the fundamental problem. When I absolutely needed to get at one particular problem font style in Qt, creating a custom version with fontforge of the font where the wanted style was renamed "normal" seemed to work.

1
votes

I've also expirienced that problem, but in mine case it was because I've added the "name" property. When I deleted name font start showing.

1
votes

The text component of QML recognizes fonts by their font name. However, if you load different font types, usually the font name within the metadata of the font is the same.

The text component has a property font.styleName, which you can use to access different types of a font:

FontLoader{id: loader source: "AwesomeFont-Bold.ttf"}
Text
{
    font.family: loader.name
    font.styleName: "Bold"
}
-3
votes

FontLoader simply works fine with me

 FontLoader{id:fixedFont; name: "Digital-7"} 
 Text {
 text:"Hello world"
 font.family: fixedFont.name
}

see here for more