[SOLVED] QML FontLoader.name confusion


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

        id: rectangle
        height: 500
        width: 700
              FontLoader { id: myCustomFont1; source: "../Fonts/Montserrat/Montserrat-ExtraLight.ttf" }
              FontLoader { id: myCustomFont2; source: "../Fonts/Montserrat/Montserrat-Black.ttf" }
                  text: "Qt for python"
                  font.family: myCustomFont1.name
                  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?


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.

Answered By – JarMan

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *