#include <QtGui>
#include <QDebug>
static const QString header
("This is the\nheader\ntext, but you can render anything here");
static const QString footer
("This is the\nfooter\ntext");
{
painter.save();
painter.resetTransform();
painter.
setFont(QFont("Courier New",
12));
QRect boundingRect
= painter.
boundingRect(painter.
window(), Qt
::AlignJustify | Qt
::TextWordWrap, header
);
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, header);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
{
painter.save();
painter.resetTransform();
painter.
setFont(QFont("Courier New",
12));
QRect boundingRect
= painter.
boundingRect(painter.
window(), Qt
::AlignJustify | Qt
::TextWordWrap, footer
);
painter.translate(0, painter.window().height() - boundingRect.height());
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, footer);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
int main(int argc, char *argv[])
{
dialog.setWindowTitle("Print Document");
if (dialog.
exec() == QDialog::Accepted) { QString lorem
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
if (painter.begin(&printer)) {
int position = 0;
// Render the first header and footer
int headerHeight = renderHeader(painter);
int footerHeight = renderFooter(painter);
position = headerHeight;
painter.translate(0, headerHeight); // I use the translation to avoid doing it manually
for (int i = 0; i < 40; ++i) {
painter.
setFont(QFont("Arial", i
));
QRect boundingRect
= painter.
boundingRect(painter.
window(),
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
// if our height is equal or greater than height of the page we introduce page break
if( position + boundingRect.height() + footerHeight > painter.window().height() ) {
printer.newPage();
headerHeight = renderHeader(painter);
footerHeight = renderFooter(painter);
position = headerHeight;
painter.resetTransform();
painter.translate(0, headerHeight);
}
// write our text
painter.drawText(boundingRect,
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
painter.translate(0, boundingRect.height());
position += boundingRect.height();
}
painter.end();
}
}
return 0;
}
#include <QtGui>
#include <QDebug>
static const QString header("This is the\nheader\ntext, but you can render anything here");
static const QString footer("This is the\nfooter\ntext");
int renderHeader(QPainter &painter)
{
painter.save();
painter.resetTransform();
painter.setFont(QFont("Courier New", 12));
QRect boundingRect = painter.boundingRect(painter.window(), Qt::AlignJustify | Qt::TextWordWrap, header);
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, header);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
int renderFooter(QPainter &painter)
{
painter.save();
painter.resetTransform();
painter.setFont(QFont("Courier New", 12));
QRect boundingRect = painter.boundingRect(painter.window(), Qt::AlignJustify | Qt::TextWordWrap, footer);
painter.translate(0, painter.window().height() - boundingRect.height());
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, footer);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPrinter printer;
QPrintDialog dialog(&printer);
dialog.setWindowTitle("Print Document");
if (dialog.exec() == QDialog::Accepted) {
QPainter painter;
QString lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
if (painter.begin(&printer)) {
int position = 0;
// Render the first header and footer
int headerHeight = renderHeader(painter);
int footerHeight = renderFooter(painter);
position = headerHeight;
painter.translate(0, headerHeight); // I use the translation to avoid doing it manually
for (int i = 0; i < 40; ++i) {
painter.setFont(QFont("Arial", i));
QRect boundingRect = painter.boundingRect(painter.window(),
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i, 10) + ") " + lorem);
// if our height is equal or greater than height of the page we introduce page break
if( position + boundingRect.height() + footerHeight > painter.window().height() ) {
printer.newPage();
headerHeight = renderHeader(painter);
footerHeight = renderFooter(painter);
position = headerHeight;
painter.resetTransform();
painter.translate(0, headerHeight);
}
// write our text
painter.drawText(boundingRect,
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i, 10) + ") " + lorem);
painter.translate(0, boundingRect.height());
position += boundingRect.height();
}
painter.end();
}
}
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks