Using custom fonts
-
Hi guys,
I'm fairly new to all of this so bear with me if this is a stupid question.How can you embed a custom font into a QApplication? I have already made a qrc file with QT Creator which looks like this when opened with Notepad++:
<RCC> <qresource prefix="/resources"> <file>resources/Triforce.ttf</file> </qresource> </RCC>
Then I compiled a rcc file with this command:
rcc -binary myresource.qrc -o myresource.rcc
as was stated here.In both the main class and the MainWindow class I've already added:
QFontDatabase::addApplicationFont("qrc:///resources/Triforce.ttf");
as well asQResource::registerResource("font.rcc");
in the main class. Still, after building my project and transferring the executable to my Virtual Machine along with the dependencies (I deploy the application with windeployqt) it doesn't use the correct font. Of course, it works perfectly fine in my regular OS since I've installed the needed font but I just can't get it to work in the VM. The ttf file is in the subdirectory "resources" as stated in the qrc file.Like I said, I'm a still a beginner with all of this, so please excuse me should I have forgotten to add required information to this post or if I did something wrong that's very obvious to you.
Thanks in advance,
Michael
-
Hey Michael,
I believe you need to set the font to the widget or QApplication (for global change).
// after adding the font with QFontDatabase // my undying love for Georgia font QFont serif("Georgia",16,QFont::Bold); widget.setFont(serif);
or
MainApplication w; // my undying love for Georgia font QFont serif("Georgia",16,QFont::Bold); // serif is a variable, change later w.setFont(serif);
Not sure if this will help you, best of luck.
-
I've put this into my MainWindow class now:
QFontDatabase::addApplicationFont("qrc:///resources/Triforce.ttf"); QFont Triforce("Triforce", 24, QFont::Normal); ui->label->setFont(Triforce);
However, my application doesn't even start now. It compiles without any errors but if I try to run it Qt Creator says "The program has unexpectedly finished." and Windows gives me its default "Programm has unfortunately stopped" message.
-
@MichiS97 said:
Hi and welcomeHave you checked what addApplicationFont returns ?
Also, a other user seemed to make it work by dynamic allocation instead of auto
when testing the font. Please see.
https://forum.qt.io/topic/23875/how-to-use-qfontdatabase-addapplicationfontAnd last note, the path for the ttf file
have you tried
QFontDatabase::addApplicationFont("resources/Triforce.ttf"))
(no qrc)Also if you right click the ttf in the project tree and select "Copy path" , and try with that one.
-
Thanks for your reply.
Using the qrc URL addApplicationFont returns -1 (which it shouldn't, I guess) as well as when I use the path "resources/Triforce.ttf". When I copy the path from the project tree addApplicationFont returns 0 but the application still crashes right away.
I'm going to look at the dynamic method, too, thanks :)
EDIT: Tried it, same result >.<
-
Could you check if the file exists within the QRC?
#include <QFile> #include <QDebug> .... QFile file(":/resources/Triforce.ttf"); qDebug() << file.exists(); QFile file(":/Triforce.ttf"); qDebug() << file.exists();
If none returns then there's a problem with the resource file.
If one of them returns true then
QFontDatabase::addApplicationFont("[string of the one that returned true]");
-
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!!