Kelvin Salton Kelvin Salton - 12 days ago 5
C++ Question

PDF report layout broken using setPaperSize(QPrinter::A4) QPrinter

I was using the following code to set the paper size of my QPrinter object:

printer_.setPaperSize(QPrinter::A4);


On my computer, it is working as expected, but on another computer, my PDF report layout is "broken".

Searching for the reason of that, I found this issue:
Qt QPrinter setPaperSize does not work properly

Like @Jens said: "setPaperSize relies on information received from the printer driver...".

I checked the
QPrinter::A4
size on Qt documentation and its size corresponds to 210 x 297 mm.

Can I just change the setPaperSize parameters from:
printer_.setPaperSize(QPrinter::A4);
to
printer_.setPaperSize(QSizeF(210, 297), QPrinter::Millimeter);
?
Will it work properly (printer independent)?

I am asking it because I don't have the other computer to test it anymore, and I can't reproduce the problem on my computer. By the way, if anyone knows how can I reproduce the problem on my computer to test if the issue was solved, I appreciate.

Example:
enter image description here

The black border on images is used only to show the boundaries of the printed area.

Note: I am using Qt 5.3.2

Answer

I changed the setPaperSize parameters from:

printer_.setPaperSize(QPrinter::A4);

to:

printer_.setPaperSize(QSizeF(210, 297), QPrinter::Millimeter);

The values 210 and 297 are from the Qt Documentation:

"QPrinter::A4 - 0 - 210 x 297 mm, 8.26 x 11.69 inches"

Basically, instead of getting the size from the printer device using QPrinter::A4, I am setting a fixed QSizeF based on the correct A4 millimeters size.

It worked for me.