Qt Printing - QFontMetrics Class

146 Views Asked by At

I'm new to Qt and exploring QPrinter / QPainter

According to the documentation, in order to print in mm, the coordinates must be translated to the printer's unit, considering it's resolution. So, I wrote the following code:

qreal printInterface::convertFromMM(qreal in)
{
    //1 inch = 25.4 mmm so
    return in * (1/25.4) * mQPrinter->resolution();
}

This is working fine for printing lines and so.

Now I must measure text, so according to the documentation I used the QFontMetrics() class.

The problem is that Painter->fontMetrics().width(stringToMeasure) is returning the width, according to the documentation, in pixels. How can I translate this to mm?

Any help will be greatly appreciated!

1

There are 1 best solutions below

0
On

Just found that I was making a mistake. I can use the same rule for FontMetrics. The documentation is, however, misleading, since it states it uses pixels as unit. It's adapted to the painter.

I've tested using two lines and printing text inside them. It measures ok both on print preview and on device.

Thanks for your time!