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: Cannot load font - Solution
Qt 6.11 is out! See what's new in the release blog

FontLoader: Cannot load font - Solution

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 951 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.
  • M Offline
    M Offline
    MapleKing
    wrote on last edited by MapleKing
    #1

    Re: QML FontLoader: Cannot load font

    Took me months of troubleshooting to solve this. Below is an example of FontLoader:

    FontLoader {
        id: myFont
        source: "myFont.ttf"
    }
    

    When you load the .qml file containing above code using qml.exe, it works fine. When you put the above code in .qml file within a Qt Quick project, and run using Qt Creator, the font doesn't load (despite copying myFont.ttf to bin directory). If you use a resource file, it still won't load. Keeps throwing the error, "QML FontLoader: Cannot load font...", or something similar. The issue is with the way resource and fonts files are processed and then embedded into the binary by CMake.

    [Solution 1]

    1. Creat a resource file and add the font to it. i.e. resources.qrc:
      <RCC>
          <qresource prefix="/fonts">
              <file>myFont.ttf</file>
          </qresource>
      </RCC>
    2. Add the resources.qrc to qt_add_qml_module() under RESOURCES:
      qt_add_qml_module(applearn-qt-quick
          URI learn-qt-quick
          VERSION 1.0
          QML_FILES
              Main.qml
          RESOURCES
              resources.qrc
      )
    3. Set CMAKE_AUTORCC variable to true in CMakeLists.txt:
          set(CMAKE_AUTORCC ON)
    4. Reference the font using the qrc url:
          source: "qrc:/fonts/myFont.ttf"

    [Solution 2]

    1. Creat a resource file and add the font to it. i.e. resources.qrc:
      <RCC>
          <qresource prefix="/fonts">
              <file>myFont.ttf</file>
          </qresource>
      </RCC>
    2. Add the resources.qrc to CMakeLists.txt using qt_add_resources():
          qt_add_resources(RCC_SOURCES resources.qrc)
      Note: above line of code should be before qt_add_executable().
    3. Add the RCC_SOURCES variable defined in step 2 to your qt_add_executable():
      qt_add_executable(applearn-qt-quick
          main.cpp
          ${RCC_SOURCES}
      )
    4. Reference the font using the qrc url:
          source: "qrc:/fonts/myFont.ttf"
      Warning: Don't add resources.qrc to qt_add_qml_module().

    [Solution 3]

    1. Add the myFont.ttf to qt_add_qml_module() under RESOURCES:
      qt_add_qml_module(applearn-qt-quick
          URI learn-qt-quick
          VERSION 1.0
          QML_FILES
              Main.qml
          RESOURCES
              myFont.ttf
      )
    2. Reference font using the follow url:
          source: "myFont.ttf"
                  or
          source: "qrc:/qt/qml/learn-qt-quick/myFont.ttf"
          Note: replace learn-qt-quick with your own project name.

    [Solution 4]

    1. Put myFont.ttf in the same directory as the app executable binary.
    2. Use the following url:
          source: "file:myFont.ttf"

    Hope this help. Thank you for your time and have a great day!

    1 Reply Last reply
    3
    • M MapleKing has marked this topic as solved on
    • C Offline
      C Offline
      cerberus77
      wrote on last edited by
      #2

      Helped a lot here, Sir! Thanks!

      1 Reply Last reply
      0
      • KH-219DesignK Offline
        KH-219DesignK Offline
        KH-219Design
        wrote on last edited by
        #3

        I had a similar issue recently where I was certain that a particular file was present in the qrc compiled resources, but the paths I was using to try to "get at" the file seemed to all be invalid guesses of the qrc file path.

        Temporarily adding this snippet and looking at all the paths helped me solve it:

        QDirIterator it(":", QDirIterator::Subdirectories);
        while (it.hasNext()) {
            qDebug() << it.next();
        }
        

        credit to: https://stackoverflow.com/questions/13509799/how-to-get-list-of-files-stored-in-a-qrc-qt-resorce-file

        www.219design.com
        Software | Electrical | Mechanical | Product Design

        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