Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Page margin about QTextDocument

Page margin about QTextDocument

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 254 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • surfciusS Offline
    surfciusS Offline
    surfcius
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • surfciusS surfcius

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @surfcius
      There is some interaction between QTextDocument 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. maybe setFullPage())?

      surfciusS 1 Reply Last reply
      1
      • JonBJ JonB

        @surfcius
        There is some interaction between QTextDocument 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. maybe setFullPage())?

        surfciusS Offline
        surfciusS Offline
        surfcius
        wrote on last edited by
        #3

        @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.
        2025-2-25a.png

        JonBJ 1 Reply Last reply
        0
        • surfciusS surfcius

          @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.
          2025-2-25a.png

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          surfciusS 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            surfciusS Offline
            surfciusS Offline
            surfcius
            wrote on last edited by
            #5

            Please 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();
            }
            
            

            tsPrint.gif

            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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved