Why QFontDatabase::addApplicationFontFromData Always return -1



  • QFile res("C:/Bruce/Projects/PCTool/source/res/font/fontawesome-webfont.ttf");
        if (res.exists() && res.open(QIODevice::ReadOnly)) {
            int fontId = QFontDatabase::addApplicationFontFromData(res.readAll());
            qDebug() << "^^^^^^^^^^^^^^^^^^^" << fontId;
        }
    

    Why this code return -1 always.

    Use : Qt5.9.1


  • Qt Champions 2017

    Hi and welcome.
    Its hard to say why it wont load it.

    Did you try with
    int id = QFontDatabase::addApplicationFont("C:/Bruce/Projects/PCTool/source/res/font/fontawesome-webfont.ttf"); ?

    Seems to use that awesome-font here

    https://stackoverflow.com/questions/30112344/how-to-embed-a-custom-font-in-my-application

    int id = QFontDatabase::addApplicationFont(":/fonts/fontawesome-webfont.ttf");
    QMessageBox::information(NULL,"Message",QString::number(id));  // this shows id is 0.
    
    QFont font;
    font.setFamily("FontAwesome");
    font.setPointSize(30);
    ui->commandLinkButton->setFont(font);
    ui->commandLinkButton->setText("\uf021"); // this shows the Refresh icon.
    

    Note: This is from a resource not directly from file. From file should work too.



  • @mrjj Thanks for reply
    I tried all kind of solution, it didn't help ether.

    The most interesting thing is before I updated QT to 5.9.1, all things went fine.
    I think there must be bugs in QT 5.9.1 about QFontDataBase


  • Qt Champions 2017

    @brucezcg

    works for me in 5.9
    alt text

    but only if messagebox is there .
    so make sure there is a some time between adding font
    and using it.



  • @mrjj Thanks a lot.
    the fontid for me still return -1
    I guess maybe the "fontawesome-webfont.ttf" file is broken, can you send me yours please.
    My email is xbruce@126.com


  • Qt Champions 2017

    @brucezcg
    Hi
    even with QMessageBox ?

    I just got font from the web. deleted it.


Log in to reply
 

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