QPrinter doesn't set margins below 12mm
-
Hi everyone,
It is the first time I am really using QPrinter. I am trying to use the full page and manage margins independent of this. However, I am not able to set margins below 12mm. Here is a minimal example:
#include <QApplication> #include <QPrinter> #include <iostream> int main(int argc, char *argv[]) { QApplication a(argc, argv); QPrinter printer; printer.setFullPage(true); QPageLayout pageLayout = printer.pageLayout(); pageLayout.setMode(QPageLayout::FullPageMode); // (1) maybe this fixes it pageLayout.setMinimumMargins(QMarginsF(0,0,0,0)); // (2) or this might fix it pageLayout.setLeftMargin(0); printer.setPageLayout(pageLayout); std::cout << "left margin: " << printer.pageLayout().margins().left() << std::endl; return 0; }
You see the options (1) and (2) where I have tried to further forcing Qt to set the margins properly. But, I always get the output
left margin: 12
I am using Qt 5.13.2 on Windows (precompiled lib with VS2017, though I am actuallly compiling my source with VS2019). Does anyone have any idea what I am missing?
-
I think I figured it out!
I assumed that everything I change in
QPrinter
directly also affects itsQPageLayout
. This is true, for example, for theQPageSize
which is always identical, i.e. if I callQPrinter::setPageSize(...)
it will change the page size of theQPageLayout
.However, this does not hold for the margins:
QPrinter::margins()
!=QPrinter::pageLayout().margins()
. Digging deaper into the documentation I have found forQPagedPaintDevice::setMargins(...)
The margins are purely a hint to the drawing method.
This is what I actually want: the margins I set should only be a hint to the drawing method and I want to control the full page. So, I will go this way. Still, I don't understand the problems I initially had.
@mrjj I don't know of any fail-proof method to select the PDF printer on Windows. Also, for the future our software should be portable to Mac and Linux again. But thanks for you help anyway.
-
Further test show that this is dependent on the printer. If I choose "Microsoft Print to PDF" instead of my default printer (which is a HP PageWide Pro) I actually get the output:
left margin: 0
But, I want this to work in any case.
-
Hi
For a real printer, it has a nonprintable area that you cannot print on.
It varies pr printer.
So you cannot print at the very edge of the paper for most printers.
(labels printer being special) -
I do know that there is a non-printable area. However, I want to initially control this aspect independent of the printer and only later select the printer (which might actually be a PDF file with no margin).
I have a first printout and since there are still some errors in the code, I accidentally printed from the top left without any margins. I turns out that for the printer where QPrinter only allows 12mm of margin it actually prints with a margin of about 4mm. Anyway, should'nt I be allowed to set the margins to anything I want when either in
FullPageMode
or after callingsetMinimumMargins(QMarginsF(0,0,0,0))
? -
@SimonSchroeder said in QPrinter doesn't set margins below 12mm:
Anyway, should'nt I be allowed to set the margins to anything I want when either in FullPageMode or after calling setMinimumMargins(QMarginsF(0,0,0,0))?
Yes that should work if you first set it to PDF + FullPageMode
so it dont look at the default printer or anything else. -
I think I figured it out!
I assumed that everything I change in
QPrinter
directly also affects itsQPageLayout
. This is true, for example, for theQPageSize
which is always identical, i.e. if I callQPrinter::setPageSize(...)
it will change the page size of theQPageLayout
.However, this does not hold for the margins:
QPrinter::margins()
!=QPrinter::pageLayout().margins()
. Digging deaper into the documentation I have found forQPagedPaintDevice::setMargins(...)
The margins are purely a hint to the drawing method.
This is what I actually want: the margins I set should only be a hint to the drawing method and I want to control the full page. So, I will go this way. Still, I don't understand the problems I initially had.
@mrjj I don't know of any fail-proof method to select the PDF printer on Windows. Also, for the future our software should be portable to Mac and Linux again. But thanks for you help anyway.
-
Ah, i mean PDF output - not a pdf installed printer.
You can print to pdf directly without one installed in OS.In any case, im not sure what you are seeing but I also had some slightly odd issue with the margins as
Inkscape could print much closer than i coul din Qt so i did wonder why but never found the actual reason. -
@mrjj I actually want to print to an actual printer. So, using a PDF in between is not an option.
Just as I said: the margins of the
QPrinter::pageLayout()
do not work as one would expect. In order to be able to print close to the border, you have to turn on full page mode and set the margins of theQPrinter
directly. I am not entirely sure (as I have a huge code base involved converting wxWidgets to Qt), but it looks like setting the margins ofQPrinter
actually prints with that margin (in contrast to what the documentation suggests). For me the problems are now solved.