Results 1 to 5 of 5

Thread: How to print the same header on multiple pages

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to print the same header on multiple pages

    Something like this:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. static const QString header("This is the\nheader\ntext, but you can render anything here");
    5. static const QString footer("This is the\nfooter\ntext");
    6.  
    7. int renderHeader(QPainter &painter)
    8. {
    9. painter.save();
    10. painter.resetTransform();
    11. painter.setFont(QFont("Courier New", 12));
    12. QRect boundingRect = painter.boundingRect(painter.window(), Qt::AlignJustify | Qt::TextWordWrap, header);
    13. painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, header);
    14. painter.drawRect(boundingRect);
    15. painter.restore();
    16. return boundingRect.height();
    17. }
    18.  
    19. int renderFooter(QPainter &painter)
    20. {
    21. painter.save();
    22. painter.resetTransform();
    23. painter.setFont(QFont("Courier New", 12));
    24. QRect boundingRect = painter.boundingRect(painter.window(), Qt::AlignJustify | Qt::TextWordWrap, footer);
    25. painter.translate(0, painter.window().height() - boundingRect.height());
    26. painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, footer);
    27. painter.drawRect(boundingRect);
    28. painter.restore();
    29. return boundingRect.height();
    30. }
    31.  
    32.  
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication app(argc, argv);
    37.  
    38.  
    39. QPrinter printer;
    40. QPrintDialog dialog(&printer);
    41. dialog.setWindowTitle("Print Document");
    42.  
    43. if (dialog.exec() == QDialog::Accepted) {
    44. QPainter painter;
    45. QString lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
    46.  
    47. if (painter.begin(&printer)) {
    48. int position = 0;
    49.  
    50. // Render the first header and footer
    51. int headerHeight = renderHeader(painter);
    52. int footerHeight = renderFooter(painter);
    53.  
    54. position = headerHeight;
    55. painter.translate(0, headerHeight); // I use the translation to avoid doing it manually
    56.  
    57. for (int i = 0; i < 40; ++i) {
    58. painter.setFont(QFont("Arial", i));
    59. QRect boundingRect = painter.boundingRect(painter.window(),
    60. Qt::AlignJustify | Qt::TextWordWrap,
    61. QString::number(i, 10) + ") " + lorem);
    62.  
    63. // if our height is equal or greater than height of the page we introduce page break
    64. if( position + boundingRect.height() + footerHeight > painter.window().height() ) {
    65. printer.newPage();
    66. headerHeight = renderHeader(painter);
    67. footerHeight = renderFooter(painter);
    68. position = headerHeight;
    69. painter.resetTransform();
    70. painter.translate(0, headerHeight);
    71. }
    72.  
    73. // write our text
    74. painter.drawText(boundingRect,
    75. Qt::AlignJustify | Qt::TextWordWrap,
    76. QString::number(i, 10) + ") " + lorem);
    77. painter.translate(0, boundingRect.height());
    78. position += boundingRect.height();
    79. }
    80.  
    81. painter.end();
    82. }
    83. }
    84.  
    85. return 0;
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 

  2. The following 2 users say thank you to ChrisW67 for this useful post:

    neda (19th January 2017), omci (10th March 2012)

  3. #2
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to print the same header on multiple pages

    Thank you for this demo, it is just what I needed. I read about save(), restore(), but did not know how to use them. Thank you also for showing me translate(), again it was a mystery for me. This will reduce my app for several thousand lines of code and make it useful again
    Regards; Luka

Similar Threads

  1. What is best way to print multiple pages long table?
    By squizzz in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2012, 20:34
  2. print of all the pages of Qt Assistant
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 3rd January 2012, 06:55
  3. print several pages
    By beirrascan in forum Newbie
    Replies: 1
    Last Post: 8th November 2010, 22:26
  4. Not print the range of pages
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2009, 17:18
  5. multiple plots on multple pages
    By giusepped in forum Qwt
    Replies: 0
    Last Post: 29th July 2009, 08:38

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.