July 28, 2011

qgil qgil
Ant Farmer
86 posts

[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

7 replies

July 28, 2011

ZapB ZapB
Robot Herder
1354 posts

In the main() function add a call to QApplication::setFont() [doc.qt.nokia.com]. Your QML elements should then use the specified font as default.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

July 28, 2011

qgil qgil
Ant Farmer
86 posts

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

  1. #include <QtGui/QApplication>
  2. #include <QtDeclarative>
  3.  
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication app(argc, argv);
  8.     QApplication::setFont("Nokia Pure Text");
  9.     QDeclarativeView view;
  10.     view.setSource(QUrl("qrc:/qml/main.qml"));
  11.     view.showFullScreen();
  12.     return app.exec();    
  13. }

…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*)

July 28, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

Try this :

  1. QFont nokiaFont;
  2. nokiaFont.setFamily("Nokia Pure Text");
  3. QApplication::setFont(nokiaFont);

 Signature 

Qt Certified Specialist
Qt Ambassador

July 28, 2011

qgil qgil
Ant Farmer
86 posts

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

July 28, 2011

Eddy Eddy
Area 51 Engineer
1296 posts

Lol
And now you’re going for the dessert!?

I’m glad I could help!

 Signature 

Qt Certified Specialist
Qt Ambassador

July 28, 2011

NielsMayer NielsMayer
Lab Rat
1 posts

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

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication app(argc, argv);
  4. #if defined(MEEGO_EDITION_HARMATTAN)
  5.     app.setFont(QFont("Nokia Pure Text"));
  6. #endif
  7. ...

July 28, 2011

ZapB ZapB
Robot Herder
1354 posts

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.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

 
  ‹‹ [SOLVED]QML internalization per file ??      UIConstants HowTo? ››

You must log in to post a reply. Not a member yet? Register here!