PDA

View Full Version : how to set custom font to qlabel



binary001
8th March 2015, 10:33
Hi,

I add mylabels to form programmatically.
Now I set custom font to these labels as follow:

1.


QFont font("MyFont001 ", 30, QFont::Bold);
myLabel->setFont(font);


or

2.


myLabel->setStyleSheet(@"font: 9pt "MyFont001 ";");


Both can not change to myLabels' font to MyFont001

So I add a label at Qt Designer and change QLabel's font to my custom font. It's also same result.

How can I set custom font (ttf font) to qlabel programmatically?
Thanks.

anda_skoa
8th March 2015, 12:09
Have you checked the font object?

The first approach is the canonical way to do that but maybe your font object is not the font you are expecting, e.g. your font not being available to the system's font database.

Cheers,
_

binary001
8th March 2015, 12:51
Thanks for your replay,

I checked my font name in font database and I found it.



QString fontName;
QFontDatabase database;
foreach (const QString &family, database.families()) {
fontName= fontName + ";" + family;
}



Finally I found a error at font.

My custom font name has a space "MyFont001 " in name. It doe not work. So I'll compile my font again.

Thank anda_skoa.