Мы начинаем с определения диапазона печати. Функции
Затем мы печатаем каждую страницу. Внешний цикл
Внутренний цикл
01 void PrintWindow::printPage(QPainter *painter,
02 const QStringList &entries, int pageNumber)
03 {
04 painter->save();
05 painter->translate(0, LargeGap);
06 foreach (QString entry, entries) {
07 QStringList fields = entry.split(": ");
08 QString title = fields[0];
09 QString body = fields[1];
10 printBox(painter, title, titleFont, Qt::lightGray);
11 printBox(painter, body, bodyFont, Qt::white);
12 painter->translate(0, MediumGap);
13 }
14 painter->restore();
15 painter->setFont(footerFont);
16 painter->drawText(painter->window(),
17 Qt::AlignHCenter | Qt::AlignBottom,
18 QString::number(pageNumber));
19 }
Функция
01 void PrintWindow::printBox(QPainter *painter, const QString &str,
02 const QFont &font, const QBrush &brush)
03 {
04 painter->setFont(font);
05 int boxWidth = painter->window().width();
06 int textWidth = boxWidth - 2 * SmallGap;
07 int maxHeight = painter->window().height();
08 QRect textRect = painter->boundingRect(SmallGap, SmallGap,
09 textWidth, maxHeight, Qt::TextWordWrap, str);
10 int boxHeight = textRect.height() + 2 * SmallGap;
11 painter->setPen(QPen(Qt::black, 2, Qt::SolidLine));
12 painter->setBrush(brush);
13 painter->drawRect(0, 0, boxWidth, boxHeight);
14 painter->drawText(textRect, Qt::TextWordWrap, str);
15 painter->translate(0, boxHeight);
16 }
Функция printBox() вычерчивает контур блока, затем отображает текст внутри него.
Графические средства OpenGL