[SOLVED] How to apply a font family as default through a Qt Quick app

  • Real use case: Nokia is encouraging developers to use Nokia Pure in apps targeting Nokia devices. Makes sense and the font looks nice. Now, how is the best way to implement this in a Qt Quick project? I could not find any hints in the QML docs and I have no idea about Qt/C++.

    I want to have Nokia Pure Text as default for all text strings. If a specific item requires a different font.family (e.g. Nokia Pure Header) then I would just need to add the parameter to the item itself.

    fwiw my first little Qt Quick pet project can be found at https://gitorious.org/testdef/testdef/trees/master/testdef

  • In the main() function add a call to "QApplication::setFont()":http://doc.qt.nokia.com/latest/qapplication.html#setFont. Your QML elements should then use the specified font as default.

  • Thanks but the builder complains. Maybe I'm still doing something wrong?

    @#include <QtGui/QApplication>
    #include <QtDeclarative>

    int main(int argc, char *argv[])
    QApplication app(argc, argv);
    QApplication::setFont("Nokia Pure Text");
    QDeclarativeView view;
    return app.exec();

    ...main.cpp:-1: In function 'int main(int, char**)':

    ...main.cpp:8: error: no matching function for call to 'QApplication::setFont(const char [16])'

    ...QtSDK/Madde/sysroots/harmattan-nokia-meego-arm-sysroot-1122-slim/usr/include/qt4/QtGui/qapplication.h:162: candidates are: static void QApplication::setFont(const QFont&, const char*)

  • Moderators

    Try this :

    @QFont nokiaFont;
    nokiaFont.setFamily("Nokia Pure Text");

  • Thanks Eddy, the builder liked your code and digested it.

  • Moderators

    And now you're going for the dessert!?

    I'm glad I could help!

  • What is the advantage of using the form Eddy posted and something like this:

    @int main(int argc, char *argv[])
    QApplication app(argc, argv);
    app.setFont(QFont("Nokia Pure Text"));

  • Eddy's code is more explicit and allows you to set additional properties on the QFont object before setting it as the application font.

    The #if defined part means that it is only compiled in if you are targeting MeeGo.

Log in to reply

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