Page margin about QTextDocument
-
wrote on 25 Feb 2025, 02:13 last edited by
I want to print the contents of a QTextDocument object to a printer. How do I set the page margins to 0 mm? Calling the QPrinter::setPageMargins(QMarginsF(0, 0, 0, 0)) method actually results in a print margin of about 20 mm.
Thank you for your reply.
-
I want to print the contents of a QTextDocument object to a printer. How do I set the page margins to 0 mm? Calling the QPrinter::setPageMargins(QMarginsF(0, 0, 0, 0)) method actually results in a print margin of about 20 mm.
Thank you for your reply.
wrote on 25 Feb 2025, 08:14 last edited by@surfcius
There is some interaction betweenQTextDocument
margins and the actual printer device margins. And printers have some sort of margins of their own. And it depends on printer output device. Have a read through QPrinter doesn't set margins below 12mm, anything there relevant to you (e.g. maybesetFullPage()
)? -
@surfcius
There is some interaction betweenQTextDocument
margins and the actual printer device margins. And printers have some sort of margins of their own. And it depends on printer output device. Have a read through QPrinter doesn't set margins below 12mm, anything there relevant to you (e.g. maybesetFullPage()
)?wrote on 25 Feb 2025, 13:13 last edited by@JonB Thank you for your reply.
I found the reason in the official Qt documentation. Please the see the official documentation screenshot. It says "By default a 2 cm margin is set around the document contents. " Now I want to know how to change the default 2cm margin.
-
@JonB Thank you for your reply.
I found the reason in the official Qt documentation. Please the see the official documentation screenshot. It says "By default a 2 cm margin is set around the document contents. " Now I want to know how to change the default 2cm margin.
wrote on 25 Feb 2025, 18:53 last edited by@surfcius
Isn't that what the link I gave you discusses? The final post from the OP there: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 the QPrinter directly.
-
@surfcius
Isn't that what the link I gave you discusses? The final post from the OP there: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 the QPrinter directly.
wrote on 26 Feb 2025, 02:50 last edited byPlease try this code:
#include <QPrinter> #include <QPrintDialog> #include <QTextDocument> #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); QTextDocument document; document.setHtml("<h1>Hello, World!</h1><p>This is a test document.</p>"); QPrinter printer; printer.setFullPage(true); printer.setPageSize(QPageSize::A4); printer.setPageMargins(QMarginsF(0, 50, 10, 10)); QPrintDialog printDialog(&printer); if (printDialog.exec() == QDialog::Accepted) { document.print(&printer); } return app.exec(); }
The above animation shows the process and results of program execution. As can be seen from the results, the left margin of the PDF document page we obtained is obviously not 0 mm. Although we have executed the following code:
QPrinter printer; printer.setFullPage(true); printer.setPageSize(QPageSize::A4); printer.setPageMargins(QMarginsF(0, 50, 10, 10));
By the way:This is a very simple program. Why does the system still warn: Invalid parameter passed to C runtime function?
Program development environment:
Product: Qt Creator 15.0.1
Desktop_Qt_6_7_3_MSVC2022_64bit-Debug
2/5