Results 1 to 4 of 4

Thread: Qt Printing QTextDocument problem (truncated text)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

  1. Replies: 3
    Last Post: 10th March 2010, 16:07
  2. Edit QTextDocument for printing
    By ericV in forum Qt Programming
    Replies: 0
    Last Post: 29th July 2009, 12:45
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. QTextDocument printing - specify font size?
    By Scorp1us in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2007, 15:25
  5. Replies: 0
    Last Post: 28th June 2006, 20: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.