Qt QPrinter setPaperSize 无法正常工作 [英] Qt QPrinter setPaperSize does not work properly

查看:41
本文介绍了Qt QPrinter setPaperSize 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 Windows 应用程序(但最终也会将其移植到 linux,因此如果可能,交叉兼容性很重要),它将从网络摄像头拍摄照片并且可以在不使用打印对话框的情况下进行打印,但我是选择纸张尺寸时遇到问题.我希望将纸张尺寸设置为 4" x 6",这是 A6 格式,但是当我使用 setPaperSize(QtPrinter::A6) 时,它似乎默认为字母格式.不过,它并不总是默认为所有打印机的字母,看起来每个打印机处理命令的方式不同,但大多数默认为字母.我认为这可能是 Qt 和打印机与驱动程序兼容性的问题.

I am trying to create a Windows application (but will eventually port it to linux also, so cross-compatibility is important if possible) that will take a picture from a webcam and can print without using a printDialog box, but I am having an issue selecting a paper size. I would like the paper size to be set to 4" x 6" which is the A6 format, but when I use setPaperSize(QtPrinter::A6) it seems to default to the letter format. It does not always default to letter with all printers though, it looks like each printer handles the command differently, but most default to letter. I believe this may be an issue with Qt and printer compatibility with the drivers.

我的问题是:有谁知道在 Qt 中将打印机设置为 4" x 6" 的方法,该方法适用于所有打印机?

My question is: Does anyone know a way to set the printer to 4" by 6" in Qt that should work with all printers?

我的代码如下所示.

void MainWindow::Print() {

    QPainter painter;
    QPrinter *printer = new QPrinter(QPrinter::HighResolution);
    printer->setPaperSize(QPrinter::A6);
     if (!painter.begin(printer)) {
         qWarning("Failed to open file");
         return;
     }

     painter.fillRect(QRectF(QPointF(108,118),QPointF(110+352, 120+352)), Qt::black);
     painter.fillRect(QRectF(QPointF(109,119),QPointF(109+352, 119+352)), Qt::white);
     ui->graphicsView->scene()->render(&painter, QRectF(110,120, 350, 350), QRectF(0,0, ui->graphicsView->scene()->width(), ui->graphicsView->scene()->height()), Qt::IgnoreAspectRatio);
     painter.drawText(110, 110, "Test");
     painter.end();
}

我尝试了以下调整纸张大小

I have tried the following for resizing the paper

printer->setPaperSize(QPrinter::A6)
printer->setPageSize(QPrinter::A6)
printer->setPaperSize(QSizeF(4.0, 6.0), QPrinter::Inch)

这些似乎都不起作用.如果有人能帮我解决这个问题,我会非常感谢

none of those seemed to work. If anyone could help me with this issue I would be very greateful

推荐答案

setPaperSize 依赖于从打印机驱动程序接收到的信息,因此要真正独立于打印机,请自行计算 pageRects.请参阅 pageRect 和 paperRect 属性以及 QPrinter 的 fullPage 属性.另请参阅我对 打印 pagerect 问题 的回答,其中存在(错误)打印任意打印矩形的起始示例以及如何修复问题给出的代码.

setPaperSize relies on information received from the printer driver, so to be really printer independant, calculare pageRects yourself. See the pageRect and the paperRect property together with the fullPage property of QPrinter. See also my answer to Printing pagerect issues where there is a (wrong) starting example of printing arbitrary print rects and how to fix the code given with the question.

这篇关于Qt QPrinter setPaperSize 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆