Results 1 to 5 of 5

Thread: How to print the same header on multiple pages

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

    Question How to print the same header on multiple pages

    Hi;

    I have to print a document of variable length and therefore have to check on several places for possible page breaks. I am using QPrinter with QPainter. That would not be a problem if I did not have to print a header and footer to every one of that pages. Is there any sane way of doing that? What would you recommend?

    Thank you for your answers! Regards; Luka

  2. #2
    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

    Every time you call newPage() output your header and footer and remove that space from the page height available to your other content.

  3. #3
    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

    I'm afraid I don't quite understand your advice. Below is a sample code. In real code I have dozen while and for loops that may or may not run. And the header and footer expand through multiple lines using graphic, text and tables. I would like to make the code for header/footer reusable, so if I make a correction I would not have to scroll through the code and possibly miss something... I also have to print several different documents that have the same header/footer. Is it possible? And how? Thanks; Luka

    Qt Code:
    1. QPrinter printer;
    2.  
    3. QPrintDialog *dialog = new QPrintDialog(&printer, this);
    4. dialog->setWindowTitle(tr("Print Document"));
    5.  
    6. if (dialog->exec() == QDialog::Accepted) {
    7. QPainter painter;
    8. QString lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
    9.  
    10. ui->lineEdit->setText(QString::number(printer.height(), 10));
    11. if (! painter.begin(&printer)) { // failed to open file
    12. qWarning("Is the output file writable?");
    13. return;
    14. }
    15.  
    16. int i = 1;
    17. int position = 0;
    18. int our_height = 0;
    19. while ( i <= 40 ) {
    20. // make text 1 pt (px?) larger
    21. painter.setFont(QFont("Arial", i));
    22.  
    23. // get size of rectangle of text
    24. QRect text_size = painter.boundingRect(0, 0, printer.width(), 0, Qt::AlignJustify | Qt::TextWordWrap, QString::number(i, 10) + ") " + lorem);
    25. // calculate the height of so far used text
    26. our_height = position + text_size.height();
    27.  
    28. // if our height is equal or greater than height of the page we introduce page break
    29. if( our_height >= printer.height() ) {
    30. printer.newPage();
    31. our_height = 0;
    32. position = 0;
    33. }
    34.  
    35. /**
    36. * How would I make this possible?
    37. **/
    38. if ( our_height == 0 ) {
    39. // write_header();
    40. // write_footer();
    41. // position = position + height_of_header + height_of_footer;
    42. }
    43.  
    44. // wrote our text
    45. painter.drawText(QRectF(0, position, printer.width(), text_size.height()), Qt::AlignJustify | Qt::TextWordWrap, QString::number(i, 10) + ") " + lorem);
    46. // set our new position
    47. i++;
    48. if (our_height == 0) {
    49. position = text_size.height();
    50. }
    51. else {
    52. position = our_height;
    53. }
    54. }
    55. painter.end();
    56. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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 

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

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

  6. #5
    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.