Load font from .ttf file



  • 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!


  • Moderators

    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.



  • 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...


  • Moderators

    [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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.