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. Load font from a file into a QFont object
Forum Updated to NodeBB v4.3 + New Features

Load font from a file into a QFont object

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 18.7k 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.
  • U Offline
    U Offline
    uranusjr
    wrote on last edited by
    #1

    Hi,

    Is there a way to create a font from a file that is not in the system's font list? For example, if I place a monospace.ttf in the resource system, say :/fonts/monospace.ttf, how can I create a QFont object from this file?

    Thanks.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi,

      Have a look at "this docnote":http://developer.qt.nokia.com/doc/qt-4.7/qfontdatabase.html#note-47.

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        Isn't addApplicationFont() static so it could/should be QFontDatabase::addApplicationFont(...)?
        Do you coincidentally know if the note about Unix/X11 and fontconfig is still valid?

        1 Reply Last reply
        0
        • U Offline
          U Offline
          uranusjr
          wrote on last edited by
          #4

          I just tried QFontDatabase::addApplicationFont(), with the following findings:

          1. I was misled a little by the note supplied by Eddy. The name used to built QFont should be the family name of the font. The note used yourfont as both the file name (in the resource file) and the family name (for QFont). But it's pretty easy to figure it out anyway. I used the following code to get the family name without knowing it in advance:

          @
          int id = QFontDatabase::addApplicationFont(":/fonts/monospace.ttf");
          QString family = QFontDatabase::applicationFontFamilies(id).at(0);
          QFont monospace(family);
          @

          1. I still couldn't get my font to work. :-( With Roman character fonts (Courier, Times, Helvecita, etc.) the above method worked flawlessly. But the font I'm trying to use is a Chinese font, and no matter how I tried, I couldn't find a way to get to work. The family name generated by the above code for that font is in Roman characters (containing spaces), which didn't work.

          If I install the font into the OS, it is shown with a Chinese family name -- I tried that, and it didn't work either. Maybe it's the encoding problem (although the source said it is a Unicode font, and the system I'm on uses Unicode). I don't know.

          Is there a way to check whether Qt really finds the family name or not? AFAIK QFont seems to have some kind of fallback mechanism for characters not in the font family, and even if I give QFont some nonsense family name, the characters would still be shown (with system default?) Is there a way to tell whether QFont really finds a font for the font family I'm giving it?

          1 Reply Last reply
          0
          • U Offline
            U Offline
            uranusjr
            wrote on last edited by
            #5

            I just found the answer on a "Chinese page":http://www.bsdmap.com/UNIX_html/XWindow.AIX/00000017/00000003.htm (smth.org is the largest Simplified Chinese BBS in the world).

            The problem is how to find the correct family name for QFont. For Roman characters, the name used for internal referring and external displaying are usually the same, but for this is not the case for Chinese fonts (and at least other CJK fonts, I guess). The author uses xlsfonts to get the internal representation of the fonts. For example, in

            @
            -misc-hanwangcc02-medium-r-normal--0-0-0-0-p-0-big5-0
            @

            you have "hanwangcc02" as the family name. Use this in the constructor of QFont, and everything will work.

            But my original purpose was to use a font not in the system. I'm still looking for a way to get the font info without loading it into the system -- doesn't fit the topic for this forum.

            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