Warm tip: This article is reproduced from serverfault.com, please click

QML FontLoader.name confusion

发布于 2020-11-28 16:55:50

I downloaded from Google Fonts two .ttf files on my project folder:

  1. Montserrat-ExtraLight.ttf
  2. Montserrat-Black.ttf

I set propperly the .qrc file in order to contain both of them.

Suppose I have the next .qml file:

import QtQuick 2.7
import QtQuick.Layouts 1.2
import QtQuick.Controls.Universal 2.0

Rectangle{
        id: rectangle
        height: 500
        width: 700
          Column{
              FontLoader { id: myCustomFont1; source: "../Fonts/Montserrat/Montserrat-ExtraLight.ttf" }
              FontLoader { id: myCustomFont2; source: "../Fonts/Montserrat/Montserrat-Black.ttf" }
              Text{
                  ...
                  text: "Qt for python"
                  font.family: myCustomFont1.name
                  ...
              }
              Text{
                  ...
                  text: "Qt for c++"
                  font.family: myCustomFont2.name
                  ...
              }
          }
        
}

The problem is that the myCustomFont1.name and the myCustomFont2.name are the same, namely "Montserrat" and I don't have any solution to make distinction between them. Therefore even if I specified the correct FontLoader-s id-s, the second text will have the same font.family like the first text. Could be possible to solve this problem somehow?

Questioner
Armonicus
Viewed
0
JarMan 2020-11-29 05:06:40

This is not an ideal solution, but a workaround that should work. There's an open-source font editor called FontForge that you can use to change the names that Qt reads. Open the font files in question and then open the menu Element->Font Info. That opens a dialog with multiple tabs on the left. The first tab should be PS Names. This should list several fields including Fontname and Family Name. You should be able to edit those to whatever you want. Then close that dialog and use File->Generate Fonts to regenerate the .ttf files.