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 .ttf file
Forum Updated to NodeBB v4.3 + New Features

Load font from .ttf file

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 11.6k 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.
  • P Offline
    P Offline
    portoist
    wrote on last edited by
    #1

    Hello,
    I am trying to load and set font from a .ttf file. I have following code:
    @
    QString fontName(QLatin1String("fonts/Ubuntu-Medium.ttf"));
    int appFontId = QFontDatabase::addApplicationFont(fontName);
    if (appFontId < 0){
    qWarning() << "Attempt to load application font: "<< fontName << " failed.";
    }
    QFile fontFile("fonts/Ubuntu-Medium.ttf");
    fontFile.open(QFile::ReadOnly);
    qDebug() << fontFile.exists();
    qDebug() << fontFile.size();
    appFontId = QFontDatabase::addApplicationFontFromData(fontFile.readAll());
    if (appFontId < 0){
    qWarning() << "Attempt to load application font: "<< fontName << " failed.";
    }
    @
    If i compile and run my project i get following output:
    @Attempt to load application font: "fonts/Ubuntu-Medium.ttf" failed.
    true
    346940
    Attempt to load application font: "fonts/Ubuntu-Medium.ttf" failed.@
    Can anyone advice me what am I doing wrong? File with font exists, but why it wont load?:-) I am using Qt 4.8 on Ubuntu 11.10.
    Thank you for any advice!

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You are using a relative path for finding the font files. Did you try to use an absolute path as well?
      Sometimes the working directory is not pointing where you think it should be.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        portoist
        wrote on last edited by
        #3

        I'v tried absolute paths but I wasn't successful. I'v tried compiling with Qt4.7.4 and it works as desired, but I would like to get it working with 4.8.0...

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [quote author="portoist" date="1324473616"]I'v tried absolute paths but I wasn't successful. I'v tried compiling with Qt4.7.4 and it works as desired, but I would like to get it working with 4.8.0...[/quote]

          If the same code works with 4.7.4 and it does not with 4.8.0 you should file a bug report on "JIRA":https://bugreports.qt.nokia.com/secure/Dashboard.jspa

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • P Offline
            P Offline
            portoist
            wrote on last edited by
            #5

            Hm I guess it won't be bug, it seems that there might be something wrong with my Qt4.8 installation. If I create new empty project with "Hello world!" its fonts are messed up. I'v tried to run some demos from qt and fonts are messed up too. I'v checked dir where Qt was installed (/usr/local/Trolltech/Qt-4.8.0) and in lib/ folder there wasn't fonts/ folder. I copied fonts folder there but fonts still look bad.

            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