Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding a TTF font to QML via QFontDatabase fails

Adding a TTF font to QML via QFontDatabase fails

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.2k Views
  • 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.
  • E Offline
    E Offline
    EStudley
    wrote on 26 Jul 2020, 17:57 last edited by
    #1

    Hello,

    I am trying to add the Roboto font to my application without having it installed by the operating system. The TTF files were downloaded directly from Google Fonts.

    Working correctly:

    • The TTF files can be correctly installed by Windows and works in QML that way. (QML family/styleName is right)
    • The TTF file is being correctly referenced from my QRC file; QFile(":/fonts/Roboto-Regular.ttf").exists() returns true.

    Not working:

    • QFontDatabase::addApplicationFont(":/fonts/Roboto-Regular.ttf") returns -1.
    • QFontDatabase::addApplicationFontFromData(QFile(":/fonts/Roboto-Regular.ttf").readAll()) returns -1.
    • The font does not appear in my QML.

    Possibly something about the complexity of the TTF file from Google Fonts? Is there any specific metadata missing?

    Thanks.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      EStudley
      wrote on 26 Jul 2020, 19:35 last edited by
      #2

      When I call:

      qDebug() << QFontDatabase::addApplicationFontFromData(QFile(":/fonts/Roboto-Regular.ttf").readAll());

      The following is output:

      QIODevice::read (QFile, ":/fonts/Roboto-Regular.ttf"): device not open
      QFSFileEngine::open: No file name specified
      -1

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bonnie
        wrote on 26 Jul 2020, 21:57 last edited by Bonnie
        #3

        Hey, are you on Linux or something?
        There's a note on the doc:

        Note: Adding application fonts on Unix/X11 platforms without fontconfig is currently not supported.

        So I think you need to install fontconfig on your system first.

        As for the QFile error: it needs to be opened for read before you can perform read on it, just as what it told you.
        But still, this function has the same note as above.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          EStudley
          wrote on 27 Jul 2020, 21:29 last edited by EStudley
          #4

          I am not on Linux unfortunately; Windows 10.

          I've discovered that explorer.exe has a constant file lock on any .ttf file on my computer instantly. I am assuming that's the cause of the failure to load.

          B 1 Reply Last reply 27 Jul 2020, 23:32
          0
          • F Offline
            F Offline
            fcarney
            wrote on 27 Jul 2020, 21:47 last edited by
            #5

            font lock issue

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1
            • F Offline
              F Offline
              fcarney
              wrote on 27 Jul 2020, 21:49 last edited by
              #6

              I am interested in the solution to this. Can you make the font be part of a qrc and use it from there?

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              0
              • E EStudley
                27 Jul 2020, 21:29

                I am not on Linux unfortunately; Windows 10.

                I've discovered that explorer.exe has a constant file lock on any .ttf file on my computer instantly. I am assuming that's the cause of the failure to load.

                B Offline
                B Offline
                Bonnie
                wrote on 27 Jul 2020, 23:32 last edited by Bonnie
                #7

                @EStudley
                I've tested in my WIndows 10, download the ttf from google fonts and add it to qrc.

                qDebug() << QFontDatabase::addApplicationFont(":/font/Roboto-Regular.ttf");
                

                this returns 0.
                So at least the code is not the problem.

                1 Reply Last reply
                0

                1/7

                26 Jul 2020, 17:57

                • Login

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