Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Qt5 build on Linux: how to handle fonts when deploying?

Tags:

c++

linux

fonts

qt

I have created static versions of Qt 5.2.0 libs with these configure options (Ubuntu 12.04):

-opensource -confirm-license -force-pkg-config -release -static -prefix '/home/juzzlin/qt5' -no-icu -opengl desktop -no-glib -accessibility -nomake examples -nomake tests -qt-zlib -qt-libpng -qt-libjpeg -qt-sql-sqlite -qt-xcb -qt-pcre -v

Now, the problem is that when I have compiled and linked my app against these Qt libs, it tries to load fonts from the Qt installation path /home/juzzlin/qt5/lib/fonts. How is this supposed to work? The app works on the machine that I used to compile it, but not on some other machine. I also don't want to install Qt stuff to some system directories with the app, as applications shouldn't do that.

This is the error I get:

QFontDatabase: Cannot find font directory /home/juzzlin/qt5/lib/fonts - is Qt installed correctly?

How can I force it to search for fonts in some other directory?

The other thing I don't understand is that why I don't have this same problem when cross-compiling for Windows with MXE? It uses practically the same configuring options when compiling Qt libs.

like image 200
juzzlin Avatar asked Dec 23 '13 21:12

juzzlin


1 Answers

You can embed the font file(s) into your executable using the Qt resource system.

http://qt-project.org/doc/qt-5/resources.html

Then in your application, you can load the embedded font.

QGuiApplication app(argc, argv);
QQuickView view;

// Load the embedded font.
QString fontPath = ":/fonts/MyFont.ttf";
int fontId = QFontDatabase::addApplicationFont(fontPath);
if (fontId != -1)
{
    QFont font("MyFont");
    app.setFont(font);
}

I suspect that your application is searching for the fonts in your home directory because qmake hard-codes the paths to different resources at compile time. To see the values of these paths, run:

qmake -query

You can override these paths in your application by including a qt.conf file, which you can also embed into the executable using the qt resource system.

http://qt-project.org/doc/qt-5.0/qtdoc/qt-conf.html

like image 127
lnthomas Avatar answered Nov 17 '22 17:11

lnthomas