PDA

View Full Version : Setting the Font in a Custom Widget



cpsmusic
13th June 2011, 13:27
Hi,

I'm currently developing a custom widget (a meter that displays realtime input). I need to draw a scale on the meter. So far I've used the following:



QFont font("Times", 8, QFont::Normal, true);
setFont(font);
for(unsigned int i = 0; i < 11; ++i) {
painter.drawLine(15, 20*i, 19, 20*i);
painter.drawText(3, 20*i + 3, QString::number(i*6));
}


I'm not happy with the font - how can I work out what fonts are available?

mvuori
13th June 2011, 13:37
How did you miss QFontDatabase? http://doc.qt.nokia.com/latest/qfontdatabase.html

cpsmusic
13th June 2011, 14:12
Thanks!

One thing about the code I posted - it seems that even if I change the font name, the font shown in the widget doesn't change. Am I missing something?

Lykurg
13th June 2011, 17:29
set the font at the QPainter object you are using to draw the text. If you call setFont outside the paint event, then it will be set the next time the paintEvent method will be called.

cpsmusic
14th June 2011, 01:13
The code I posted is called inside the widget's paintEvent method.

Santosh Reddy
14th June 2011, 08:18
You can set the font to the painter


QFont font("Times", 8, QFont::Normal, true);
painter->setFont(font);