May 29, 2011

goocreations goocreations
Ant Farmer
161 posts

Stylesheet: linking custom fonts

 

Hi all

I have a specific font stored in a file (ttf). Now I want to link this font to my QLabel’s font. I’ve done the following:

  1. label->setStyleSheet("font-family: url(C:/font.tff);");

But this doesn’t seem to work. Does anyone know how to link an external font to your Qt application’s style sheet?

5 replies

May 29, 2011

Peppy Peppy
Hobby Entomologist
389 posts

label->setFont() ?

May 29, 2011

Chuck Gao Chuck Gao
Lab Rat
342 posts

Try QFontDatabase from Qt C++ or FontLoader from QML :)

Try:

  1. int QFontDatabase::addApplicationFont ( const QString & fileName ) [static]
  2. QStringList QFontDatabase::applicationFontFamilies ( int id ) [static]

  1. import QtQuick 1.0
  2.  
  3.  Column {
  4.      FontLoader { id: fixedFont; name: "Courier" }
  5.      FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" }
  6.  
  7.      Text { text: "Fixed-size font"; font.family: fixedFont.name }
  8.      Text { text: "Fancy font"; font.family: webFont.name }
  9.  }

 Signature 

Chuck

May 29, 2011

goocreations goocreations
Ant Farmer
161 posts

cool, thanks

November 2, 2011

Hassan Kassem Hassan Kassem
Lab Rat
7 posts

@Chuck:I have tried these two methods and it doesn’t work on symbian
The first method always return -1
I am using Qt 4.7.3

February 12, 2012

kjaer kjaer
Lab Rat
2 posts

Chuck, I think it’s important to know whether QT C++ uses True Type Fonts already installed in the user’s computer or if we can deploy our chosen TTF, to be used in a computer that doesn’t have this TTF previously installed.

It seems a stupid doubt, but QT docs aren’t clear enough – specially for beginners, like me. I don’t know if I can use a specially designed personal TTF in my software (it would make my life easier, by using only Text Classes) or if I have to use bitmap images to get the exact text design I want in all plataforms.

As there are differences about TTF handling in Linux… and also because sometimes we need to use a special TTF in our programs, TTF deployment without the need to install a new font in the target computer would be a very useful feature to me.

I don’t want to use QML – I am too old to learn a new language. In my old times, C++ was tough enough to be learned and in the present, I confess that QT libraries are being almost too much to be explored by a STL-er. :)

What can you and other experts say about?

(Please, forgive my awful english. It is not my mother language)

Valdemar Katayama Kjaer
Brazil

Chuck Gao wrote:
Try QFontDatabase from Qt C++ or FontLoader from QML :)

Try:

  1. int QFontDatabase::addApplicationFont ( const QString & fileName ) [static]
  2. QStringList QFontDatabase::applicationFontFamilies ( int id ) [static]

@import QtQuick 1.0

Column { FontLoader { id: fixedFont; name: “Courier” } FontLoader { id: webFont; source: “http://www.mysite.com/myfont.ttf” } Text { text: “Fixed-size font”; font.family: fixedFont.name } Text { text: “Fancy font”; font.family: webFont.name } }@
 
  ‹‹ qtextstream in(QFile) does not read      Crypt a QByteArray without external dipendences [Solved] ››

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