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. QPrinter doesn't set margins below 12mm
Forum Updated to NodeBB v4.3 + New Features

QPrinter doesn't set margins below 12mm

Scheduled Pinned Locked Moved Solved General and Desktop
qprintermargins
9 Posts 3 Posters 2.9k Views 2 Watching
  • 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.
  • S Offline
    S Offline
    SimonSchroeder
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SimonSchroeder
      wrote on last edited by
      #6

      I think I figured it out!

      I assumed that everything I change in QPrinter directly also affects its QPageLayout. This is true, for example, for the QPageSize which is always identical, i.e. if I call QPrinter::setPageSize(...) it will change the page size of the QPageLayout.

      However, this does not hold for the margins: QPrinter::margins() != QPrinter::pageLayout().margins(). Digging deaper into the documentation I have found for QPagedPaintDevice::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.

      mrjjM 1 Reply Last reply
      2
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #2

        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.

        mrjjM 1 Reply Last reply
        0
        • S SimonSchroeder

          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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @SimonSchroeder

          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)

          1 Reply Last reply
          3
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #4

            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 calling setMinimumMargins(QMarginsF(0,0,0,0))?

            mrjjM 1 Reply Last reply
            0
            • S SimonSchroeder

              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 calling setMinimumMargins(QMarginsF(0,0,0,0))?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #6

                I think I figured it out!

                I assumed that everything I change in QPrinter directly also affects its QPageLayout. This is true, for example, for the QPageSize which is always identical, i.e. if I call QPrinter::setPageSize(...) it will change the page size of the QPageLayout.

                However, this does not hold for the margins: QPrinter::margins() != QPrinter::pageLayout().margins(). Digging deaper into the documentation I have found for QPagedPaintDevice::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.

                mrjjM 1 Reply Last reply
                2
                • S SimonSchroeder

                  I think I figured it out!

                  I assumed that everything I change in QPrinter directly also affects its QPageLayout. This is true, for example, for the QPageSize which is always identical, i.e. if I call QPrinter::setPageSize(...) it will change the page size of the QPageLayout.

                  However, this does not hold for the margins: QPrinter::margins() != QPrinter::pageLayout().margins(). Digging deaper into the documentation I have found for QPagedPaintDevice::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.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @SimonSchroeder

                  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.

                  S 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @SimonSchroeder

                    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.

                    S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #8

                    @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 the QPrinter 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 of QPrinter actually prints with that margin (in contrast to what the documentation suggests). For me the problems are now solved.

                    surfciusS 1 Reply Last reply
                    1
                    • JonBJ JonB referenced this topic on
                    • S SimonSchroeder

                      @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 the QPrinter 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 of QPrinter actually prints with that margin (in contrast to what the documentation suggests). For me the problems are now solved.

                      surfciusS Offline
                      surfciusS Offline
                      surfcius
                      wrote on last edited by
                      #9
                      This post is deleted!
                      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