Qt QPrinter setPaperSize does not work properly

自闭症网瘾萝莉.ら 提交于 2020-01-16 01:15:07

问题


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.

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

My code is shown below.

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


回答1:


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.



来源:https://stackoverflow.com/questions/14881382/qt-qprinter-setpapersize-does-not-work-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!