gpt4 book ai didi

c++ - Qt QPainter 以毫米而不是英寸为单位

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:29 25 4
gpt4 key购买 nike

我有一个 QPrinter,可以将 A4 直接打印到物理打印机或 PDF。现在我想用 QPainter 以毫米为单位绘图,但当前坐标系似乎是 A4 的宽度和高度(以英寸为单位)乘以打印机的分辨率。

8.26 英寸 x 1200 分辨率 = 9912
11.69 英寸 x 1200 分辨率 = 14028

我已经尝试了以下方法,但文本最终变得很大。

auto page = printer.pageRect(QPrinter::Unit::Millimeter);
painter.setWindow(QRect(0, 0, page.width(), page.height()));

如何更改此设置以便我的 QPainter 可以绘制到 210 x 297 毫米而不是上述系统?

这是在 Windows 10 和 Qt 5.10 上。

最佳答案

我在 X11 (ubuntu linux) PDF 打印上测试了这个方法,使用 ScreenResolution 打印机模式:

painter.begin(printer);

int log_w = 210;
int log_h = 297;
painter.setWindow(0, 0, log_w, log_h);

int phys_w = printer->width();
int phys_h = printer->height();
painter.setViewport(0, 0, phys_w, phys_h);

基本上,使用画家窗口以毫米为单位设置您的逻辑尺寸,并为画家的视口(viewport)提供打印机的物理尺寸。此行应在页面周围打印一个边框为 10 毫米的矩形:

painter.drawRect(10, 10, log_w - 20, log_h -20);

文本应该相应地工作。此代码应在矩形的左上角打印单词 Ok:

  QFont font = painter.font();
font.setPointSize(10); //1 cm height
painter.setFont(font);
painter.drawText(10, 20, "Ok");

painter.end();

使用 HighResolution 打印机模式,必须使用

设置字体大小
font.setPixelSize(10); //1 cm height

并且 QPen 必须设置为画家:

QPen pen(Qt::black);
pen.setWidthF(0.2);
painter.setPen(pen);
painter.drawRect(10, 10, log_w - 20, log_h - 20);

关于使用 setPixelSize 丢失设备依赖性,我知道 here声明:

It is possible to set the height of characters shown on the screen to a specified number of pixels with setPixelSize(); however using setPointSize() has a similar effect and provides device independence.

但我认为它仅指屏幕,因为here声明:

When rendering text on a QPrinter device, it is important to realize that the size of text, when specified in points, is independent of the resolution specified for the device itself. Therefore, it may be useful to specify the font size in pixels when combining text with graphics to ensure that their relative sizes are what you expect.

关于c++ - Qt QPainter 以毫米而不是英寸为单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47734545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com