Results 1 to 1 of 1

Thread: Howto : Header and Footers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2007
    Posts
    36
    Thanks
    5
    Thanked 3 Times in 1 Post

    Default Howto : Header and Footers

    This code will print the html header and footer. (suggestions welcomed)

    Qt Code:
    1. QRect printer_rect(printer->pageRect());
    2.  
    3. ///Setting up the header and calculating the header size
    4. QTextDocument *document_header = new QTextDocument(this);
    5. document_header->setPageSize(printer_rect.size());
    6. document_header->setHtml(company_header);
    7. QSizeF header_size = document_header->size();
    8.  
    9. ///Setting up the footer and calculating the footer size
    10. QTextDocument *document_footer = new QTextDocument(this);
    11. document_footer->setPageSize(printer_rect.size());
    12. document_footer->setHtml(company_footer);
    13. QSizeF footer_size = document_footer->size();
    14.  
    15. ///Calculating the main document size for one page
    16. QSizeF center_size(printer_rect.width(), (printer->pageRect().height() - header_size.toSize().height() - footer_size.toSize().height()));
    17.  
    18. ///Setting up the center page
    19. QTextDocument *main_doc = new QTextDocument(this);
    20. main_doc->setHtml(content);
    21. main_doc->setPageSize(center_size);
    22.  
    23. ///Setting up the rectangles for each section.
    24. QRect headerRect = QRect(QPoint(0,0), document_header->size().toSize());
    25. QRect footerRect = QRect(QPoint(0,0), document_footer->size().toSize());
    26. QRect contentRect = QRect(QPoint(0,0), main_doc->size().toSize()); /// Main content rectangle.
    27. QRect currentRect = QRect(QPoint(0,0), center_size.toSize()); /// Current main content rectangle.
    28.  
    29. QPainter painter(printer);
    30.  
    31. while (currentRect.intersects(contentRect))
    32. {///Loop if the current content rectangle intersects with the main content rectangle.
    33. ///Resetting the painter matrix co ordinate system.
    34. painter.resetMatrix();
    35. ///Applying negative translation of painter co-ordinate system by current main content rectangle top y coordinate.
    36. painter.translate(0, -currentRect.y());
    37. ///Applying positive translation of painter co-ordinate system by header hight.
    38. painter.translate(0, headerRect.height());
    39. ///Drawing the center content for current page.
    40. main_doc->drawContents(&painter, currentRect);
    41. ///Resetting the painter matrix co ordinate system.
    42. painter.resetMatrix();
    43. ///Drawing the header on the top of the page
    44. document_header->drawContents(&painter, headerRect);
    45. ///Applying positive translation of painter co-ordinate system to draw the footer
    46. painter.translate(0, headerRect.height());
    47. painter.translate(0, center_size.height());
    48. document_footer->drawContents(&painter, footerRect);
    49.  
    50. ///Translating the current rectangle to the area to be printed for the next page
    51. currentRect.translate(0, currentRect.height());
    52. ///Inserting a new page if there is till area left to be printed
    53. if (currentRect.intersects(contentRect))
    54. {
    55. printer->newPage();
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

  2. The following 3 users say thank you to pshah.mumbai for this useful post:

    KMAPSRULE (22nd July 2009), nifei (3rd March 2009), sipahi (11th February 2013)

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.