简体   繁体   English

Qt Printing-QFontMetrics类

[英]Qt Printing - QFontMetrics Class

I'm new to Qt and exploring QPrinter / QPainter 我是Qt的新手,正在探索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. 根据文档,为了以mm进行打印,必须考虑坐标的分辨率将坐标转换为打印机的单位。 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. 现在我必须测量文本,因此根据文档,我使用了QFontMetrics()类。

The problem is that Painter->fontMetrics().width(stringToMeasure) is returning the width, according to the documentation, in pixels. 问题在于, Painter->fontMetrics().width(stringToMeasure)根据文档返回Painter->fontMetrics().width(stringToMeasure)以像素为单位)。 How can I translate this to mm? 如何将其翻译为mm?

Any help will be greatly appreciated! 任何帮助将不胜感激!

Just found that I was making a mistake. 刚刚发现我在犯错。 I can use the same rule for FontMetrics. 我可以对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! 谢谢你的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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