- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
Is it possible to set a header and footer when printing a QTextDocument?
There is no API for this at the moment, so you will have to implement this yourself. In order to do this manually, what needs to be done is to print the header and footer yourself and use QTextDocument::drawContents() [doc.qt.nokia.com] to draw the relevant part of the page that will fit on the paper. In order to do this you need to calculate what area of the page rectangle your header and footer will occupy and the rest is used for the content of the document. By moving the rectangle that gets what needs to be painted each time and translating the painter so that is effectively painting at 0×0 then it will paint the contents for that page as drawContents() [doc.qt.nokia.com] will clip the painting to the specified rectangle. This is put into a while loop that checks that the rectangle to be painted is actually intersecting with the complete contents rectangle.
The following is an example of how it can be implemented:
- #include <QtGui>
- int main(int argc, char **argv)
- {
- f.close();
- QTextDocument td;
- td.setHtml(content);
- QPrinter p;
- pd.exec();
- td.setPageSize(p.pageRect().size());
- innerRect.setTop(innerRect.top() + 20);
- innerRect.setBottom(innerRect.bottom() - 30);
- int count = 0;
- painter.save();
- painter.translate(0, 30);
- while (currentRect.intersects(contentRect)) {
- td.drawContents(&painter, currentRect);
- count++;
- currentRect.translate(0, currentRect.height());
- painter.restore();
- painter.save();
- painter.translate(0, -currentRect.height() * count + 30);
- if (currentRect.intersects(contentRect))
- p.newPage();
- }
- painter.restore();
- painter.end();
- return 0;
- }

4 comments
January 12, 2012
Lab Rat
Thank you. This also helps with my problem – I want to print or paint different parts of the QTextDocument onto a QGraphicsView, where I control the position of the “windows” or “viewports” onto the document.
From the documentation it wasn’t clear to how the rect was clipping the document in QTextDocument::drawContent(QPainter*, const QRectF). The example definitely clarifies that.
May 2, 2012
Lab Rat
Really useful!
But I was wonderring if it is possible to change the size of the text of the header and/or the footer
August 13, 2012
Lab Rat
It is also possible to use a toplevel QTextTable and mark the first row as a header:
March 25, 2013
Lab Rat
If the printer is set to QPrinter::HighResolution then you may need to add the following line:
just before
so that the scaling between the text document and printer is done correctly.