View Full Version : Howto : Header and Footers

29th October 2008, 16:32
This code will print the html header and footer. (suggestions welcomed)

QRect printer_rect(printer->pageRect());

///Setting up the header and calculating the header size
QTextDocument *document_header = new QTextDocument(this);
QSizeF header_size = document_header->size();

///Setting up the footer and calculating the footer size
QTextDocument *document_footer = new QTextDocument(this);
QSizeF footer_size = document_footer->size();

///Calculating the main document size for one page
QSizeF center_size(printer_rect.width(), (printer->pageRect().height() - header_size.toSize().height() - footer_size.toSize().height()));

///Setting up the center page
QTextDocument *main_doc = new QTextDocument(this);

///Setting up the rectangles for each section.
QRect headerRect = QRect(QPoint(0,0), document_header->size().toSize());
QRect footerRect = QRect(QPoint(0,0), document_footer->size().toSize());
QRect contentRect = QRect(QPoint(0,0), main_doc->size().toSize()); /// Main content rectangle.
QRect currentRect = QRect(QPoint(0,0), center_size.toSize()); /// Current main content rectangle.

QPainter painter(printer);

while (currentRect.intersects(contentRect))
{///Loop if the current content rectangle intersects with the main content rectangle.
///Resetting the painter matrix co ordinate system.
///Applying negative translation of painter co-ordinate system by current main content rectangle top y coordinate.
painter.translate(0, -currentRect.y());
///Applying positive translation of painter co-ordinate system by header hight.
painter.translate(0, headerRect.height());
///Drawing the center content for current page.
main_doc->drawContents(&painter, currentRect);
///Resetting the painter matrix co ordinate system.
///Drawing the header on the top of the page
document_header->drawContents(&painter, headerRect);
///Applying positive translation of painter co-ordinate system to draw the footer
painter.translate(0, headerRect.height());
painter.translate(0, center_size.height());
document_footer->drawContents(&painter, footerRect);

///Translating the current rectangle to the area to be printed for the next page
currentRect.translate(0, currentRect.height());
///Inserting a new page if there is till area left to be printed
if (currentRect.intersects(contentRect))