Using custom fonts
-
Damn....neither of the two exist....what could I have done wrong with the resource file?
EDIT: Got it!! Seems like you shouldn't choose the same name for the folder in which the ttf is as the prefix for your qrc file. Now qDebug returns true but it sill won't work...
EDIT2: I just don't understand anything anymore. Like I said in the first edit, qDebug returned true for both paths. Well....it did...once. I proceeded to use Clean All and QMake afterwards and now it doesn't work anymore.
-
Hi I think it maybe be related to the rc file somehow.
I added a qrc to a project and then directly added the ttf file.
and then in mainwindow constructorint loadedFontID = QFontDatabase::addApplicationFont ( ":/Triforce.ttf" ); QFont Triforce("Triforce", 24, QFont::Normal); ui->label->setFont(Triforce);
and it just worked. ?! So I think you are really close to nailing it :)
-
Been wondering, are you using linux? windows? osx? If linux you need to have fontconfig installed (according to documentation)
I'm sort of running out of suggestions, hopefully someone senior appears as like you I've recently started on this journey with C++/Qt .
I guess my last suggestion would be instead of calling addapplicationfont call
QFile file(":/Triforce.ttf"); // the one that returned true qDebug () << QFontDatabase::addApplicationFontFromData(file.readAll());
As for the implications of loading it like thatI wouldn't know. Best of luck.
-
@MichiS97
Could you try this mini test ?
https://www.dropbox.com/s/h75uamsxonvvczx/myhappyfont.zip?dl=0Works here on win 7.
-
Exactly what mrjj says, QtCreator handles resources just fine there's no need to compile it manually. You can add your
qrc
to your.pro
file and avoid all those headaches. Moreover, you can edit/add files and prefixes as you see fit through QtCreator.I tested the app since I got curious. http://i.imgur.com/mhtj26i.png
One thing to keep in mind. If you add text under Rich Text, QT will respect the HTML generated and it will ignore any attempts to manipulate it. Remove the HTML in the label if there's any, else it will never render with the selected font.
-
@David.G I did everything qrc related with the Qt Creator...
The only thing I compiled myself was the rcc file since that was mentioned in the wiki. Seeing as how mrjj's app works fine without it I've deleted the rcc though.@mrjj I tried it with your qrc as well (even though I knew that they were exactly the same) and I still get the same error
Update: heh....I got it to work...seems to be a bad idea to do the font stuff before the setupUi line....seems obvious enough once you see it. I've already tried it in my VM and yes, it really works now! Thanks for all your help!!