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. Footer and page number for multiple printing using QTextCursor.
Forum Updated to NodeBB v4.3 + New Features

Footer and page number for multiple printing using QTextCursor.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 991 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I made a specific form using QTextCursor and when the page turns, even if the page number is attached to the bottom left, can you delete the page number or change the position?
    And I want to set footer and header, how can I access it?

    eyllanescE 1 Reply Last reply
    0
    • I IknowQT

      @jsulm

      this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
      

      If you annotate this part, it can be printed, but the screen layout will be broken.

      If not annotated, the pdf file is normally created and processed, but the program dies over time.

      8088820e-72f8-4cff-9d05-bd955a883918-image.png
      Call stack tracking is not possible either.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

      this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);

      You probably need to undo that line after printing (set the previously set paint device). The problem is: "print" is local variable and is deleted as soon as it gets out of scope. If Qt tries to paint again it uses a deleted object.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      1
      • I IknowQT

        I made a specific form using QTextCursor and when the page turns, even if the page number is attached to the bottom left, can you delete the page number or change the position?
        And I want to set footer and header, how can I access it?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #2

        @IknowQT I see it complicated since you will see that the implementation cannot be changed from the outside: https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qtextdocument.cpp?h=5.12.11#n1934, my recommendation is that you use QWebEnginePage: create your form using html where you can place the header and footer with html, css and js.

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        I 1 Reply Last reply
        0
        • eyllanescE eyllanesc

          @IknowQT I see it complicated since you will see that the implementation cannot be changed from the outside: https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/text/qtextdocument.cpp?h=5.12.11#n1934, my recommendation is that you use QWebEnginePage: create your form using html where you can place the header and footer with html, css and js.

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @eyllanesc

          https://stackoverflow.com/questions/44537788/page-x-of-y-using-qprinter/44540195#44540195

          QPrinter print(QPrinter::PrinterResolution);
              print.setOutputFormat(QPrinter::PdfFormat);
              print.setPageSize(QPrinter::A4);
              print.setOutputFileName("Output.pdf");
          
              QPainter painter(&print);
              this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
              this->ui.textEdit->document()->setPageSize(print.pageRect().size());
              QSizeF pageSize = print.pageRect().size(); // page size in pixels
          
              //Calculate the rectangle where to lay out the text
              const double tm = mmToPixels(print, 12);
              const qreal footerHeight = painter.fontMetrics().height();
              const QRectF textRect(tm, tm, pageSize.width() - 2 * tm, pageSize.height() - 2 * tm - footerHeight);
              this->ui.textEdit->document()->setPageSize(textRect.size());
          
              const int pageCount = this->ui.textEdit->document()->pageCount();
          
              bool firstPage = true;
              for (int pageIndex = 0; pageIndex < pageCount; ++pageIndex) {
          
                  if (!firstPage)
                      print.newPage();
          
                  PaintPage(pageIndex, pageCount, &painter, this->ui.textEdit->document(), textRect, footerHeight);
                  firstPage = false;
              }
          

          02378556-94fb-4211-8557-0c0938a8f436-image.png

          this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
          

          I found your article while searching. I applied it like this. Can you let me know if an error occurs after generating a pdf file?
          I think the code is the problem, so what should I fix?

          jsulmJ 1 Reply Last reply
          0
          • I IknowQT

            @eyllanesc

            https://stackoverflow.com/questions/44537788/page-x-of-y-using-qprinter/44540195#44540195

            QPrinter print(QPrinter::PrinterResolution);
                print.setOutputFormat(QPrinter::PdfFormat);
                print.setPageSize(QPrinter::A4);
                print.setOutputFileName("Output.pdf");
            
                QPainter painter(&print);
                this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
                this->ui.textEdit->document()->setPageSize(print.pageRect().size());
                QSizeF pageSize = print.pageRect().size(); // page size in pixels
            
                //Calculate the rectangle where to lay out the text
                const double tm = mmToPixels(print, 12);
                const qreal footerHeight = painter.fontMetrics().height();
                const QRectF textRect(tm, tm, pageSize.width() - 2 * tm, pageSize.height() - 2 * tm - footerHeight);
                this->ui.textEdit->document()->setPageSize(textRect.size());
            
                const int pageCount = this->ui.textEdit->document()->pageCount();
            
                bool firstPage = true;
                for (int pageIndex = 0; pageIndex < pageCount; ++pageIndex) {
            
                    if (!firstPage)
                        print.newPage();
            
                    PaintPage(pageIndex, pageCount, &painter, this->ui.textEdit->document(), textRect, footerHeight);
                    firstPage = false;
                }
            

            02378556-94fb-4211-8557-0c0938a8f436-image.png

            this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
            

            I found your article while searching. I applied it like this. Can you let me know if an error occurs after generating a pdf file?
            I think the code is the problem, so what should I fix?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

            so what should I fix?

            What problem do you have?!

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            I 1 Reply Last reply
            0
            • jsulmJ jsulm

              @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

              so what should I fix?

              What problem do you have?!

              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #5

              @jsulm

              this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
              

              If you annotate this part, it can be printed, but the screen layout will be broken.

              If not annotated, the pdf file is normally created and processed, but the program dies over time.

              8088820e-72f8-4cff-9d05-bd955a883918-image.png
              Call stack tracking is not possible either.

              jsulmJ 2 Replies Last reply
              0
              • I IknowQT

                @jsulm

                this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
                

                If you annotate this part, it can be printed, but the screen layout will be broken.

                If not annotated, the pdf file is normally created and processed, but the program dies over time.

                8088820e-72f8-4cff-9d05-bd955a883918-image.png
                Call stack tracking is not possible either.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

                but the program dies over time

                Then please use debugger.

                "Call stack tracking is not possible either" - why not? Are you using debug build?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                I 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

                  but the program dies over time

                  Then please use debugger.

                  "Call stack tracking is not possible either" - why not? Are you using debug build?

                  I Offline
                  I Offline
                  IknowQT
                  wrote on last edited by
                  #7

                  @jsulm

                  43192d7c-6f86-4193-8b85-2c53f8ce159d-image.png

                  Call stack information cannot be followed up because the program ends inside qt.

                  1 Reply Last reply
                  0
                  • I IknowQT

                    @jsulm

                    this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);
                    

                    If you annotate this part, it can be printed, but the screen layout will be broken.

                    If not annotated, the pdf file is normally created and processed, but the program dies over time.

                    8088820e-72f8-4cff-9d05-bd955a883918-image.png
                    Call stack tracking is not possible either.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

                    this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);

                    You probably need to undo that line after printing (set the previously set paint device). The problem is: "print" is local variable and is deleted as soon as it gets out of scope. If Qt tries to paint again it uses a deleted object.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    I 1 Reply Last reply
                    1
                    • jsulmJ jsulm

                      @IknowQT said in Footer and page number for multiple printing using QTextCursor.:

                      this->ui.textEdit->document()->documentLayout()->setPaintDevice(&print);

                      You probably need to undo that line after printing (set the previously set paint device). The problem is: "print" is local variable and is deleted as soon as it gets out of scope. If Qt tries to paint again it uses a deleted object.

                      I Offline
                      I Offline
                      IknowQT
                      wrote on last edited by
                      #9

                      @jsulm

                      You were right! It's solved! Thank you, but why is the layout broken after clicking Print?

                      e50c5e50-4bee-4626-a0c8-4704210d592b-image.png

                      9c69f941-3d9e-4239-9ad9-783ccd923672-image.png

                      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