简体   繁体   English

在Linux上构建静态Qt5:如何在部署时处理字体?

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

I have created static versions of Qt 5.2.0 libs with these configure options (Ubuntu 12.04): 我已经使用这些配置选项(Ubuntu 12.04)创建了Qt 5.2.0库的静态版本:

-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 -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 . 现在,问题是当我编译并链接我的应用程序对这些Qt库时,它尝试从Qt安装路径/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. 我也不想将Qt东西安装到应用程序的某些系统目录中,因为应用程序不应该这样做。

This is the error I get: 这是我得到的错误:

QFontDatabase: Cannot find font directory /home/juzzlin/qt5/lib/fonts - is Qt installed correctly? QFontDatabase:找不到字体目录/ home / juzzlin / qt5 / lib / fonts - 是否正确安装了Qt?

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 ? 另一件我不明白的是,为什么我在使用MXE交叉编译Windows时没有遇到同样的问题? It uses practically the same configuring options when compiling Qt libs. 它在编译Qt库时使用几乎相同的配置选项。

You can embed the font file(s) into your executable using the Qt resource system. 您可以使用Qt资源系统将字体文件嵌入到可执行文件中。

http://qt-project.org/doc/qt-5/resources.html 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. 我怀疑您的应用程序正在搜索主目录中的字体,因为qmake在编译时硬编码到不同资源的路径。 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. 您可以通过包含qt.conf文件来覆盖应用程序中的这些路径,您也可以使用qt资源系统将其嵌入到可执行文件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM