简体   繁体   中英

Qt Printing - QFontMetrics Class

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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM