Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. FontLoader. How it work?
Qt 6.11 is out! See what's new in the release blog

FontLoader. How it work?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 6 Posters 7.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lolopolosko
    wrote on last edited by
    #1

    I have custom font
    I load this font in FontLoader (for example in MyForm.qml)
    I create new QML document and use my custom font . (MySecondForm.qml)

    If I load my font in MyForm.qml his available using in MySecondForm?
    FontLoader loaded font in global environment?

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xargs1
      wrote on last edited by
      #2

      You can use a singleton to make it available globally.

      1 Reply Last reply
      0
      • David.GD Offline
        David.GD Offline
        David.G
        wrote on last edited by
        #3

        In your cpp file before loading the QML entry file you can just call QFontDatabase and add it. I'm going to assume the font isn't being downloaded from the internet (using a URL in the FontLoader)

        #include <QFontDatabase>
        ....
        QFontDatabase fontDB;
        fontDb.addApplicationFont("://res/fonts/mycustomfont.otf");
        
        // load QML main file
        // now you can just call the font globally on any QML file. 
        Text {
        font.family: "Source Sans Pro" 
        text: "Hello world"
        }
        

        That's how I've done it, it's been working pretty well.

        S 1 Reply Last reply
        1
        • MonomixM Offline
          MonomixM Offline
          Monomix
          wrote on last edited by Monomix
          #4

          I like the global singleton approach, too. An example would be:

          In a directory called "fonts", Fonts.qml:

          pragma Singleton
          
                 import QtQuick 2.5
          
          QtObject {
          
              property FontLoader applicationFont: FontLoader {
                  source: "OpenSans-Regular.ttf"
              }
          

          In a qmldir file in the "fonts" folder:

          singleton Fonts Fonts.qml
          

          Then, in your QML files you can use it as follows:

          import "fonts"
          
          Label {
              ...
              font.family:  Fonts.applicationFont.name;
              ...
          }
          
          1 Reply Last reply
          1
          • David.GD David.G

            In your cpp file before loading the QML entry file you can just call QFontDatabase and add it. I'm going to assume the font isn't being downloaded from the internet (using a URL in the FontLoader)

            #include <QFontDatabase>
            ....
            QFontDatabase fontDB;
            fontDb.addApplicationFont("://res/fonts/mycustomfont.otf");
            
            // load QML main file
            // now you can just call the font globally on any QML file. 
            Text {
            font.family: "Source Sans Pro" 
            text: "Hello world"
            }
            

            That's how I've done it, it's been working pretty well.

            S Offline
            S Offline
            seyed
            wrote on last edited by
            #5

            @David.G thank you. Your suggested solution is great. someone has another trick like solution here .

            1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              FontLoader just calls QGuiApplication:: addApplicationFont, it's basically the same as doing it in C++.

              You can use it in another QML file if you know the family name of the font.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved