Results 1 to 4 of 4

Thread: Qt Printing QTextDocument problem (truncated text)

  1. #1
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Qt Printing QTextDocument problem (truncated text)

    Hello guys,

    I' trying to print a QTextDocument with custom header and footer.
    I found this nice code: http://developer.qt.nokia.com/faq/an..._qtextdocument

    It works great, except that sometimes the text gets truncated on half page, so that it's not readable... Look here the result: http://dl.dropbox.com/u/1511663/test.pdf between page 2 and 3!

    I think this is something related to page size.. But I don't know how it can be fixed, what is the problem?

    Hope you can help with some hints. I don't expect you write full code for me, but I hope you can help me with some hints!

    My full code is attached as test.zip

    Thanks

    ps. this is a great forum, thanks!

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QApplication a(argc, argv);
    6. QString content;
    7.  
    8. content.append("here my html");
    9.  
    10. td.setHtml(content);
    11. //p.setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
    12.  
    13. bool pdf = true; //use pdf as output?
    14.  
    15. if (pdf) {
    16. QString outFile = QFileDialog::getSaveFileName(0, "Save PDF file",
    17. QDir::homePath(),
    18. "PDF files (*.pdf)");
    19. if (outFile.isEmpty())
    20. exit(1);
    21.  
    22. if (!outFile.contains(".pdf"))
    23. outFile.append(".pdf");
    24.  
    25. p.setOutputFileName(outFile);
    26. } else {
    27. QPrintDialog pd(&p, 0);
    28. pd.exec();
    29. }
    30.  
    31. td.setPageSize(p.pageRect().size());
    32. QRect innerRect = p.pageRect();
    33. innerRect.setTop(innerRect.top() + 20);
    34. innerRect.setBottom(innerRect.bottom() - 30);
    35. QRect contentRect = QRect(QPoint(0,0), td.size().toSize());
    36. QRect currentRect = QRect(QPoint(0,0), innerRect.size());
    37. QPainter painter(&p);
    38. int count = 0;
    39. painter.save();
    40. painter.translate(0, 30);
    41. while (currentRect.intersects(contentRect)) {
    42. td.drawContents(&painter, currentRect);
    43. count++;
    44. currentRect.translate(0, currentRect.height());
    45. painter.restore();
    46. painter.drawText(10, 10, QString("Header %1").arg(count));
    47. painter.drawText(10, p.pageRect().bottom() - 10, QString("Footer %1").arg(count));
    48. painter.save();
    49. painter.translate(0, -currentRect.height() * count + 30);
    50. if (currentRect.intersects(contentRect))
    51. p.newPage();
    52. }
    53. painter.restore();
    54. painter.end();
    55. return 0;
    56. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Qt Printing QTextDocument problem (truncated text)

    Out of curiosity what happens if you call td.setpagesize before calling td.sethtml ?

    You may need to use the QAbstractTextDocumentLayout::draw function instead of the one from QTextDocument to support things like page size td.documentLayout()->draw()




    Ok I am changing my answer lol I just looked a bit closer at your code

    The page size of the textdocument should be set to the size of the QRect you are painting it on you can't just go chopping at the paint rectangle and not expect clipping

    Qt Code:
    1. QRect innerRect = p.pageRect();
    2. innerRect.setTop(innerRect.top() + 20);
    3. innerRect.setBottom(innerRect.bottom() - 30);
    4. QRect contentRect = QRect(QPoint(0,0), td.size().toSize());
    5. QRect currentRect = QRect(QPoint(0,0), innerRect.size());
    6. td.setPageSize(currentRect.size());
    To copy to clipboard, switch view to plain text mode 
    Last edited by Berryblue031; 19th April 2011 at 12:34.

  3. The following user says thank you to Berryblue031 for this useful post:

    giowck (19th April 2011)

  4. #3
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Printing QTextDocument problem (truncated text)

    Thank you,

    I solved this by adding a margin to the printer:

    p.setPageMargins(20, 20, 20, 20, QPrinter::Millimeter);



    Added after 1 27 minutes:


    Okay so now i found the working code. But now I want to paint a second QTextDocument with the same code (while loop) but i don't know how to do it, because the code was taken from that link (see above)

    my code
    Qt Code:
    1. printer->setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
    2.  
    3. QTextDocument mainReport;
    4. mainReport.setHtml(*this->mainReportTextHtml);
    5. mainReport.setPageSize(printer->pageRect().size());
    6.  
    7. QRect innerRect = printer->pageRect();
    8. innerRect.setTop(innerRect.top());
    9. innerRect.setBottom(innerRect.bottom());
    10.  
    11. QRect contentRect = QRect(QPoint(0,0), mainReport.size().toSize());
    12. QRect currentRect = QRect(QPoint(0,0), innerRect.size());
    13.  
    14. QPainter painter(printer);
    15. int pageCount = 0;
    16.  
    17. // main report
    18. painter.save();
    19. painter.translate(0, 30);
    20.  
    21. while (currentRect.intersects(contentRect)) {
    22. mainReport.drawContents(&painter, currentRect);
    23. pageCount++;
    24. currentRect.translate(0, currentRect.height());
    25. painter.restore();
    26. painter.drawText(10, 10, QString("Header %1").arg(pageCount));
    27. painter.drawText(10, printer->pageRect().bottom() - 10, QString("Footer %1").arg(pageCount));
    28. painter.save();
    29. painter.translate(0, - currentRect.height() * pageCount + 30);
    30. if (currentRect.intersects(contentRect))
    31. printer->newPage();
    32. }
    33.  
    34. //now print the second textdocument
    35. test.setHtml(*this->secondReport);
    36. test.setPageSize(printer->pageRect().size());
    37.  
    38. printer->newPage();
    39. painter.restore();
    40. painter.save();
    41. painter.translate(0, 30);
    42.  
    43. contentRect = QRect(QPoint(0,0), test.size().toSize());
    44. currentRect = QRect(QPoint(0,0), innerRect.size());
    45.  
    46. while (currentRect.intersects(contentRect)) {
    47. test.drawContents(&painter, currentRect);
    48. pageCount++;
    49. currentRect.translate(0, currentRect.height());
    50. painter.restore();
    51. painter.drawText(10, 10, QString("Header %1").arg(pageCount));
    52. painter.drawText(10, printer->pageRect().bottom() - 10, QString("Footer %1").arg(pageCount));
    53. painter.save();
    54. painter.translate(0, - currentRect.height() * pageCount + 30);
    55. if (currentRect.intersects(contentRect))
    56. printer->newPage();
    57. }
    58.  
    59. painter.restore();
    60. painter.end();
    To copy to clipboard, switch view to plain text mode 

    The problem: only the first page of the second qtextdocument gets printed followed by blank pages.
    I mean I just want to reuse that working code for another qtextdocument...

    Is there any Qt printing guru out there who can help me?

    Thank you guys!
    Last edited by giowck; 19th April 2011 at 18:36.

  5. #4
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Printing QTextDocument problem (truncated text)

    Solved this!

    The problem was that the pageCount variable was used to calculate the offset for the draw.
    Because i dind't set pageCount=0 for the new second QTextDocument, the offset result was wrong.

    SO i introduced the new variable offset=0 for each new qtextdocument and now i works!

    complete code:
    Qt Code:
    1. printer->setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
    2.  
    3. QTextDocument mainReport;
    4. mainReport.setHtml(*this->mainReportTextHtml);
    5. mainReport.setPageSize(printer->pageRect().size());
    6.  
    7. QRect innerRect = printer->pageRect();
    8. innerRect.setTop(innerRect.top());
    9. innerRect.setBottom(innerRect.bottom());
    10.  
    11. QRect contentRect = QRect(QPoint(0,0), mainReport.size().toSize());
    12. QRect currentRect = QRect(QPoint(0,0), innerRect.size());
    13.  
    14. QPainter painter(printer);
    15. int pageCount = 0;
    16.  
    17. // main report
    18. painter.save();
    19. painter.translate(0, 30);
    20.  
    21. while (currentRect.intersects(contentRect)) {
    22. mainReport.drawContents(&painter, currentRect);
    23. pageCount++;
    24. currentRect.translate(0, currentRect.height());
    25. painter.restore();
    26. painter.drawText(10, 10, QString("Header %1").arg(pageCount));
    27. painter.drawText(10, printer->pageRect().bottom() - 10, QString("Footer %1").arg(pageCount));
    28. painter.save();
    29. painter.translate(0, - currentRect.height() * pageCount + 30);
    30. if (currentRect.intersects(contentRect))
    31. printer->newPage();
    32. }
    33.  
    34. //now print the second textdocument
    35. test.setHtml(*this->proteinMeatTableHtml);
    36. test.setPageSize(printer->pageRect().size());
    37.  
    38. printer->newPage();
    39. painter.restore();
    40. painter.save();
    41. painter.translate(0, 30);
    42.  
    43. contentRect = QRect(QPoint(0,0), test.size().toSize());
    44. currentRect = QRect(QPoint(0,0), innerRect.size());
    45. int offset = 0;
    46.  
    47. while (currentRect.intersects(contentRect)) {
    48. test.drawContents(&painter, currentRect);
    49. pageCount++;
    50. offset++;
    51. currentRect.translate(0, currentRect.height());
    52. painter.restore();
    53. painter.drawText(10, 10, QString("Header %1").arg(pageCount));
    54. painter.drawText(10, printer->pageRect().bottom() - 10, QString("Footer %1").arg(pageCount));
    55. painter.save();
    56. painter.translate(0, - currentRect.height() * offset + 30);
    57. if (currentRect.intersects(contentRect))
    58. printer->newPage();
    59. }
    60.  
    61. painter.restore();
    62. painter.end();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 10th March 2010, 17:07
  2. Edit QTextDocument for printing
    By ericV in forum Qt Programming
    Replies: 0
    Last Post: 29th July 2009, 13:45
  3. Replies: 1
    Last Post: 3rd September 2008, 15:16
  4. QTextDocument printing - specify font size?
    By Scorp1us in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2007, 16:25
  5. Replies: 0
    Last Post: 28th June 2006, 21:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.