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. Trying to add custom fonts with QFontDatabase::addApplicationFont it returns -1
Forum Update on Monday, May 27th 2025

Trying to add custom fonts with QFontDatabase::addApplicationFont it returns -1

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 3.4k 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.
  • B Offline
    B Offline
    Black Imp
    wrote on 23 Nov 2018, 17:14 last edited by
    #1

    I'm refactoring a desktop application - qt 5.7 vs2013 - which uses custom fonts.
    At the moment it loads file "Code New Roman.otf" and it works - addApplicationFont returns 0 and I'm able to visualize fonts correctly.
    I've then added some font files in the same directory which is APPLICATIONNAME\fonts then in selected in Project tree fonts directory "Add Existing Files..." and I selected one.
    In code I replaced the name of Code New Roman with the new one

    int id = QFontDatabase::addApplicationFont(":/fonts/Roboto-Light.ttf");

    it always returns -1. I tried with different fonts, otf, ttf... it never works.

    Any idea? Thank you

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 23 Nov 2018, 19:24 last edited by
      #2

      Are you sure the font is really in your resource file under :/fonts ? Did you check it with a full path name on your local filesystem instead?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • B Offline
        B Offline
        Black Imp
        wrote on 23 Nov 2018, 20:00 last edited by
        #3

        I put the font files in the right directory by explorer. Then I added them to app resources by that QtCreator's menu entry. I put it exactly where Code New Roman - the only custom font which works.
        I 'm sure about where I put the files in filesystem. I'm not sure QtCreator understand it

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 23 Nov 2018, 20:08 last edited by
          #4

          You can pass an absolute path to addApplicationFont().

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Black Imp
            wrote on 23 Nov 2018, 23:32 last edited by
            #5

            ok but what's the point? the files are in the same place as the one that's working and in the real application I obviously don't want to use an absolute path.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Nov 2018, 07:21 last edited by
              #6

              It's for testing if it works in general ...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              B 1 Reply Last reply 24 Nov 2018, 17:33
              1
              • C Christian Ehrlicher
                24 Nov 2018, 07:21

                It's for testing if it works in general ...

                B Offline
                B Offline
                Black Imp
                wrote on 24 Nov 2018, 17:33 last edited by Black Imp
                #7

                @Christian-Ehrlicher Good call, thank you. If I pass the absolute path it works. weird. I think it depends on how I added this file to qt Creator. I remember I had some issues with the other font as well on one computer and I fixed somehow adding again that file to the project but I'm not sure how

                int id = QFontDatabase::addApplicationFont("E:/Sviluppo/AppName/appNameSubDir/fonts/Roboto-Light.ttf"); // itworks

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 24 Nov 2018, 17:52 last edited by
                  #8

                  Ok, then maybe your resource file path is wrong. You can check it with QFileInfo::exists

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Black Imp
                    wrote on 26 Nov 2018, 04:23 last edited by Black Imp
                    #9

                    SOLVED: I previously added fonts by rightclicking on /fonts/fonts folder in Projects panel, then "Add Existing Files...".

                    This time I added fonts file by rightclicking directly on AppName.qrc, "Add Existing Files...". And files compare under "/" subdirectory called "fonts" not under "/fonts" which is another folder sibling to "/" in hierarchy. Probably old programmers messed up the resource tree structure but qtCreator behaviour about it is not straight or intuitive.

                    Thank you for assistance.

                    1 Reply Last reply
                    0

                    2/9

                    23 Nov 2018, 19:24

                    7 unread
                    • Login

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