1

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Armonicus
  • 59
  • 8

2 Answers2

2

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.

JarMan
  • 7,589
  • 1
  • 10
  • 25
  • Well, thx for this app. I installed it on my Mac and I changed those fields as per your instructions. But now the FontLoader name is absolutely blank. Doesn't have any name ... – Armonicus Nov 28 '20 at 23:53
  • Not sure why that would be. I haven’t had that problem when I’ve done it. – JarMan Nov 29 '20 at 16:42
  • Great. Hope it works until you find a better solution. – JarMan Nov 29 '20 at 23:18
  • After I played around with the new FontForge app I realized where should be made the modification: should navigate such as: ```File->Element->Font Info``` as you said and there shouldn't touch anything in ```PS Names``` but rather should go in ```TTF Names``` and in that particular table if exists the row _Preferred Family_, should modify the name as per desire. If not, should create a new row named like that and fill there the new desired _Font Name_. Thanks again. I will close the question considering good enough your answer. – Armonicus Nov 29 '20 at 23:25
1

This is perplexing and a common source of frustration. It turns out that the name property actually specifies the family, which as you've discovered, is the same for these font files.

What distinguishes them is actually the styleName.

Try opening the font file in a font viewer like "Font Book" or "FontForge" to get the exact styleName - you'll need to specify it with a string.

Then specify the additional property:

          Text{
              ...
              text: "Qt for python"
              font.family: myCustomFont1.name //or myCustomFont2.name, it doesn't matter.
              font.styleName: "Extra Light"
              ...
          }
          Text{
              ...
              text: "Qt for c++"
              font.family: myCustomFont2.name
              font.styleName: "Black"
              ...
          }

I've found styleName far more predictable than combinations of weight or style or bold. And that way you can work with fonts that follow the canonical naming conventions rather than hacking their Family Name to suit QML.

Heath Raftery
  • 3,643
  • 17
  • 34
  • Thanks! You saved my day on an issue where text would be rendered garbled (selecting wrong glyphs, so readable but wrong letters) – Amfasis Jan 12 '23 at 12:26