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. creating pdf document with qtextdocument and qpainter (translate)

creating pdf document with qtextdocument and qpainter (translate)

Scheduled Pinned Locked Moved General and Desktop
qpainter qtextd
9 Posts 3 Posters 5.5k 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.
  • P Offline
    P Offline
    pvip
    wrote on last edited by
    #1

    Hi,

    currently I am working on a project where I have to generate a pdf export.

    The first idea was to create a QTextDocument representing the whole stuff and call QTextDocument::print to generate the PDF. With that solution I had problems with page breaks and tables were split somewhere.

    The second idea was creating small QTextDocuments of parts and check whether there is enough space left on the current page and if not call QPrinter::newPage otherwise call QTextDocument::drawContens.

    With that solution I am able controlling the page breaks but QPainter::translate does not seem to work. Each QTextDocument is printed ontop each other.

    QPainter painter;
    painter.begin(&printer);
    for(Project p:projects) {
    
        for(Issue i:p.getIssues()) {
            
            QTextDocument doc;
            doc.setPageSize(printer.pageRect().size());
            QTextCursor cur(&doc);
            
            cur.insertBlock(format);
            drawIssue(i, cur);
            
            float height = doc.documentLayout()->documentSize().height();
            
            qDebug() << "cur " << curHeight + height;
            qDebug() << printer.pageRect().height();
            qDebug();
            
            curHeight += height;
            
            painter.save();
            QRect contentRect(0,0,printer.pageRect().width(), curHeight);
            qDebug() << contentRect;
            
            painter.translate(contentRect.topLeft());
            doc.drawContents(&painter, contentRect);
            painter.restore();
            
            pageCounter++;
            
        }
        
    }
    painter.end();
    

    Anybody an idea?

    Best regards :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Have you tried calling QPrinter::newPage() in the loop?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      P 1 Reply Last reply
      0
      • M Offline
        M Offline
        MrBlueSky125
        wrote on last edited by
        #3

        Also, calling translate with a contentRect.topLeft point of 0,0, does not do much :-)

        P 1 Reply Last reply
        0
        • M mcosta

          Have you tried calling QPrinter::newPage() in the loop?

          P Offline
          P Offline
          pvip
          wrote on last edited by
          #4

          @mcosta
          Yes if I do so, I get a new page as requested, thats fine.
          But I like to draw as many as possible parts to each page.

          One QTextDocument fills about 1/3 of an A4 page. So I need to print 3 doc's each page and than I will call newPage. The code above is a snippet to show my current problem only.

          1 Reply Last reply
          0
          • M MrBlueSky125

            Also, calling translate with a contentRect.topLeft point of 0,0, does not do much :-)

            P Offline
            P Offline
            pvip
            wrote on last edited by
            #5

            @MrBlueSky125

            Yes your right but I thought of either calling QPainter::translate or provide the height inside the contentRect.
            I tried both ways.

            QPainter::translate moves the origin of the painter to the new position. After that I have to draw on the origin position.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pvip
              wrote on last edited by pvip
              #6

              Ok I think I have mixed both ideas and ended up in a not working solution °° ^^

              This is working like expected

              QPainter painter;
              painter.begin(&printer);
              for(Project p:projects) {
              
                  for(Issue i:p.getIssues()) {
              
                      QTextDocument doc;
                      doc.setPageSize(printer.pageRect().size());
                      QTextCursor cur(&doc);
              
                      cur.insertBlock(format);
                      drawIssue(i, cur);
              
                      float height = doc.documentLayout()->documentSize().height();
                      curHeight += height + space;
              
                      if(curHeight > printer.pageRect().height()) {
                          printer.newPage();
                          curHeight = height + space;
                      }
              
                      painter.save();
                      QRect contentRect(0,0,printer.pageRect().width(), curHeight);
                      painter.translate(QPoint(0, curHeight - height));
                      doc.drawContents(&painter, contentRect);
                      painter.restore();
              
                      pageCounter++;
              
                  }
              
              }
              painter.end();
              
              1 Reply Last reply
              0
              • M Offline
                M Offline
                MrBlueSky125
                wrote on last edited by
                #7

                Hmm, in your example you use the contentRect only to clip what you draw.
                No translation, no scaling

                P 1 Reply Last reply
                0
                • M MrBlueSky125

                  Hmm, in your example you use the contentRect only to clip what you draw.
                  No translation, no scaling

                  P Offline
                  P Offline
                  pvip
                  wrote on last edited by pvip
                  #8

                  @MrBlueSky125 said:

                  Hmm, in your example you use the contentRect only to clip what you draw.
                  No translation, no scaling

                  Yes your right.
                  Have a look to my last comment.

                  And pointing to a contentRect is not needed anymore since I have defined the QTextDocument pageSize.
                  Translating only is sufficient.

                  Thank you all :)

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mcosta
                    wrote on last edited by
                    #9

                    HI,

                    don't forget to mark the thread as [SOLVED]

                    Once your problem is solved don't forget to:

                    • Mark the thread as SOLVED using the Topic Tool menu
                    • Vote up the answer(s) that helped you to solve the issue

                    You can embed images using (http://imgur.com/) or (http://postimage.org/)

                    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