打印 pageRect 和 paperRect 问题
我有以下打印代码:
void Print(QPrinter *printer)
{
QPainter q(printer);
q.setRenderHint(QPainter::HighQualityAntialiasing, true);
q.setPen(QPen(QColor("red")));
q.drawRect(printer->pageRect());
q.drawLine(printer->pageRect().topRight(), printer->pageRect().bottomLeft());
q.setPen(QPen(QColor("blue")));
q.drawRect(printer->paperRect());
q.drawLine(printer->paperRect().topRight(), printer->paperRect().bottomLeft());
}
结果与 QPrintPreviewDialog、渲染 PDF、输出到打印机(HP LaserJet、PdfFactory、PdfCreator)不同。 大多数时候,生成的矩形是没有纸的。 如何使所有打印机的输出都相似?
I have following printing code:
void Print(QPrinter *printer)
{
QPainter q(printer);
q.setRenderHint(QPainter::HighQualityAntialiasing, true);
q.setPen(QPen(QColor("red")));
q.drawRect(printer->pageRect());
q.drawLine(printer->pageRect().topRight(), printer->pageRect().bottomLeft());
q.setPen(QPen(QColor("blue")));
q.drawRect(printer->paperRect());
q.drawLine(printer->paperRect().topRight(), printer->paperRect().bottomLeft());
}
The result is different with QPrintPreviewDialog, rendered PDF, output to printers (HP LaserJet, PdfFactory, PdfCreator). Most of time the resulting rectangle is out of paper. What to do so the output is similar to all printers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是前一段时间被问到的,但我从上面获取了代码,花了我几个小时才弄清楚它出了什么问题。
首先,查看Qt源代码,我找不到任何假设5%利润的地方。
Qt 从 Windows XP 中正确读回页边距,您可以完全信任页边距(带有 Windows XP 的 Qt 4.5.3)。
上面的代码包含两个问题:要打印 paperrect,必须设置 fullPage 选项。 那么 paperRect 至少可以正确打印在预览上。 当然不是在真正的打印机上,因为它位于 pageRect 之外。
一般来说,打印 paperRect 是没有意义的,因为如果打印正确,它就会恰好位于纸张边框上。
第二个主要问题源于打印 pageRect 而不更正原点。
如果禁用 fullPage(默认),则 pageRect 原点位于 paperRect 原点,因此包括边距。
但打印从 QPoint(leftMargin,topMargin) 开始,因此边距被添加两次。
要解决此问题,需要调用 pageRect.moveTo(0,0),然后 pageRect 会在其所属位置很好地打印。
不同的打印机显示不同的结果来自于不同的设备边距。 只有具有 0 边距的设备才能使用原始代码。
This really was asked some time ago, but I took the code from above and it cost me some hours to figure out, what's wrong with it.
First, looking at the Qt source code, I could not find any place which assumes a 5% margin.
Qt reads back the margins correctly from Windows XP and you can completely trust the page margins (Qt 4.5.3 with Windows XP).
The code above contains two issues: for printing a paperrect, the fullPage option must be set. Then the paperRect is printed at least on the preview correctly. Of course not on a real printer, as it is outside the pageRect.
Generally, printing the paperRect makes no sense, as, if printed correctly, it lies exactly on the paper border.
The second major issue stems from printing pageRect without correcting the origin.
If fullPage is disabled (default), then the pageRect origin lies at the paperRect origin thus includes the margins.
But printing starts at QPoint(leftMargin,topMargin), so the margin is added twice.
To fix the issue, pageRect.moveTo(0,0) needs to be called and then the pageRect prints nicely where it belongs.
That different printers show different results comes from different device margins. Only devices with 0-margin will work with the original code.
我审阅的 Qt 代码假定 5% 的纸张作为其边距。 没有人相信正确的页边距。
The Qt code I reviewed assumes 5% of paper as it's margin. Nobody trusts on correct paper margins.