PDA

View Full Version : How do I set a custom page/paper size in QPrinter



alferjaani
22nd July 2010, 18:35
hi

How do I set a custom page/paper size in QPrinter?

Lykurg
22nd July 2010, 19:03
What about using QPrinter::setPaperSize(const QSizeF &paperSize, Unit unit)?

LautaroMed
25th July 2013, 05:38
Sorry for bringing this up, but I'm using QT 4.8 and it doesn't let me use that function, even though it's still present in the documentation.

¿Any clues? I've set my pagesize to A7, but it's a little large, and I would like to be able to customize it, in case I have to change it...

EDIT: I've just set my default printer to adobe pdf to see the output and it completely ignored the size settings I made.

ChrisW67
25th July 2013, 06:06
What does "it doesn't let me use that function" mean, especially give that your next sentence says you could set the page size?

This, for example, compiles and runs fine under Qt 4.8.4:


#include <QApplication>
#include <QPrinter>
#include <QPainter>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QPrinter printer;
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("test.pdf");
printer.setPaperSize(QSizeF(2, 2), QPrinter::Inch);

QPainter p(&printer);
p.drawLine(QPointF(0, 0), QPointF(72, 72));
p.end();

return 0;
}



$ pdfinfo test.pdf
Title:
Creator:
Producer: Qt 4.8.4 (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
CreationDate: Thu Jul 25 05:16:10 2013
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 144 x 144 pts
Page rot: 0
File size: 1358 bytes
Optimized: no
PDF version: 1.4

LautaroMed
25th July 2013, 06:43
I was using setPageSize I think.

Anyway, I tried your code, and in my pc it generates an A4 size PDF:


Title:
Creator:
Producer: Qt 4.8.1 (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
CreationDate: 07/25/13 05:36:18
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 212 x 284 pts
File size: 1348 bytes
Optimized: no
PDF version: 1.4

Here's the code I was using earlier:



QTextDocument document;
document.setHtml(strStream);

QPrinter printer;

printer.setColorMode(QPrinter::GrayScale);
printer.setPaperSize(QPrinter::A7);
qDebug() << printer.paperSize();

printer.setFullPage(false);

document.print(&printer);


The qDebug line prints "30", whatever that means...

ChrisW67
25th July 2013, 07:12
QPrinter::Custom 30 Unknown, or a user defined size.

You are setting a paper size of A7 which may not be valid for the default system printer. What does paperRect() return afterward?
The PDF size you gave, 212 x 284 pts, is 74.8 x 100.2 mm... a shade wider and a few mm shorter than A7 portrait.

QTextDocument maintains its own page size that may be passed to the printer.

Have you tried the most recent Qt 4.8?

LautaroMed
26th July 2013, 02:56
paperRect() returns "QRect(0,0 283x378)"

I am setting paper size like this now:


printer.setPaperSize(QSizeF(75, 100), QPrinter::Millimeter);

I am using QT 4.8.1 because it's the only SDK I could find...

ChrisW67
26th July 2013, 04:42
http://qt-project.org/downloads
There's a Qt 4.8.4 bundle suitable for use with the old SDK's GCC 4.4, and the MingW builds of Qt 5.1 come with a MingW GCC 4.8 toolchain included.