I am creating a custom Qt widget that mimics an A4 printed page and am having problems getting fonts to render at the correct size. My widget uses QPainter::setViewport
and QPainter::setWindow
to mimic the A4 page, using units of 10ths of a millimetre which enables me to draw easily. However, attempting to create a font at a specific point size doesn't seem to work and using QFont:setPixelSize
isn't accurate. Here is some code:
View::View(QWidget *parent) :
QWidget(parent),
printer(new QPrinter)
{
printer->setPaperSize(QPrinter::A4);
printer->setFullPage(true);
}
void View::paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.setWindow(0, 0, 2100, 2970);
painter.setViewport(0, 0, printer->width(), printer->height());
// Draw a rect at x = 1cm, y = 1cm, 6cm wide and 1 inch high
painter.drawRect(100, 100, 600, 254);
// Create a 72pt (1 inch) high font
QFont font("Arial");
font.setPixelSize(254);
painter.setFont(font);
// Draw in the same box
// The font is too large
painter.drawText(QRect(100, 100, 600, 254), tr("Wg\u0102"));
// Ack - the actual font size reported by the metrics is 283 pixels!
const QFontMetrics fontMetrics = painter.fontMetrics();
qDebug() << "Font height = " << fontMetrics.height();
}
So I'm asking for a 254 high font (1 inch, 72 pts) and it's too big and sure enough when I query for the font height via QFontMetrics
it is 283 high.
Does anyone else know how to use font sizes in points when using custom mapping modes like this? It must be possible. Note that I cannot see how to convert between logical/device points either (i.e. the Win32 DPtoLP
/LPtoDP
equivalents.)
EDIT: Well, it turns out that my code was working fine after all. I converted it to work with a printer, printed it out and then printed the same text using various word processors and the results are exactly the same. It seems that asking for a font size doesn't take the descent into account and this seems to be the norm.