How to add footer in QTextTable?
-
For the table header and page footer this approach is ok. In addition to those I, sometimes, do something like this:
for ledgers or other financial reports.
QTextDocument
is similar to theFlowDocument
of WPF BUT for these kind of reports I use WPF'sFixedDocument
, where I've full control over every page. Is there any other class, besidesQTextDocument
, which I can use to control each page individually?With
QPainter
andQPrinter
these are difficult, isn't it? -
Hi,
Depending on what exactly you want, you should check the various reporting framework built on top of Qt.
-
Hi,
Depending on what exactly you want, you should check the various reporting framework built on top of Qt.
@SGaist, sometimes I/we desire something that is harder to do with those reporting framework and for that the last resort should be the root framework (in this case Qt). I used SAP Crystal in .net and Jasper, for a while, in Java for experiment, all these, including those you referred earlier in another thread, are ok for general purpose.
-
With the previous failed approach of adding header it's close again:
on the first page I get 56 rows, excluding header, and the total gets a separate page. From the third page onward same thing, total gets its own page BUT every other full page gets 57 rows, excluding header. Code is almost similar to the previous one:
Window::Window(QWidget *parent) : QWidget(parent){ document = new QTextDocument(); document->setUndoRedoEnabled(false); document->setDocumentMargin(0); printer = new QPrinter(); printer->setPageSize(QPageSize::A4); printer->setPageMargins(QMarginsF(72,72,72,72), QPageLayout::Point); printer->setResolution(fontMetrics().fontDpi()); auto pageRect = printer->pageRect(QPrinter::DevicePixel).size(); document->setPageSize(QSizeF(pageRect.width(), pageRect.height())); createDocument(); auto lay = new QVBoxLayout(this); auto button = new QPushButton("Pint PDF", this); lay->addWidget(button); //printer->setFullPage(true); connect(button, &QPushButton::clicked, [=]{ printer->setOutputFormat(QPrinter::PdfFormat); printer->setOutputFileName("test.pdf"); int pageCount = document->pageCount(); QRectF textRect(0, 0, pageRect.width(), pageRect.height()); QPainter painter(printer); for (int pageNo = 0; pageNo < pageCount; pageNo++) { if (pageNo > 0) printer->newPage(); const QRectF textPageRect(0, pageNo * document->pageSize().height(), document->pageSize().width(), document->pageSize().height()); painter.save(); painter.translate(0, -textPageRect.top()); document->drawContents(&painter, QRectF(0, textPageRect.top(), textRect.width(), textRect.height())); painter.restore(); QRectF footerRect(0, textRect.height(), textRect.width(), fontMetrics().height()); painter.drawLine(footerRect.left(), footerRect.top(), footerRect.right(), footerRect.top()); painter.drawText(footerRect, Qt::AlignVCenter | Qt::AlignRight, QObject::tr("Page %1/%2").arg(pageNo+1).arg(pageCount)); } }); } void Window::addHeader(QTextCursor& cursor){ QTextTableCellFormat cellFormat; cellFormat.setTopBorder(1); cellFormat.setBottomBorder(1); cellFormat.setBorderBrush(Qt::black); cellFormat.setTopBorderStyle(QTextFrameFormat::BorderStyle_Solid); cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid); QTextBlockFormat rightAlign; rightAlign.setAlignment(Qt::AlignVCenter | Qt::AlignRight); QTextCharFormat textFormat; textFormat.setFontWeight(QFont::Bold); for (int i = 0; i < 3; i++) { auto cell = cursor.currentTable()->cellAt(0, i); cell.setFormat(cellFormat); if(i == 0) cursor.insertText("Column " + QString::number(i + 1), textFormat); else{ cursor.setBlockFormat(rightAlign); cursor.insertText("Column " + QString::number(i + 1), textFormat); } cursor.movePosition(QTextCursor::NextCell); } } void Window::addFooter(QTextCursor& cursor){ QTextTableCellFormat cellFormat; cellFormat.setTopBorder(1); cellFormat.setBottomBorder(1); cellFormat.setBorderBrush(Qt::black); cellFormat.setTopBorderStyle(QTextFrameFormat::BorderStyle_Solid); cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid); QTextBlockFormat rightAlign; rightAlign.setAlignment(Qt::AlignVCenter | Qt::AlignRight); QTextCharFormat textFormat; textFormat.setFontWeight(QFont::Bold); QLocale locale; for (int i = 0; i < 3; i++) { auto cell = cursor.currentTable()->cellAt(cursor.currentTable()->rows() - 1, i); cell.setFormat(cellFormat); if(i == 0) cursor.insertText("Total", textFormat); else{ cursor.setBlockFormat(rightAlign); cursor.insertText(locale.toString(1000)); } cursor.movePosition(QTextCursor::NextCell); } } QTextTableFormat Window::format(bool pageBreak){ QTextTableFormat tableFormat; tableFormat.setBorder(0); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(0); tableFormat.setHeaderRowCount(0); QVector<QTextLength> columnLengths; columnLengths.append(QTextLength(QTextLength::PercentageLength, 80)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 10)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 10)); if(pageBreak) tableFormat.setPageBreakPolicy(QTextTableFormat::PageBreak_AlwaysBefore); tableFormat.setColumnWidthConstraints(columnLengths); return tableFormat; } void Window::createDocument(){ int row = 200, column = 3; QTextCursor cursor(document); cursor.insertTable(1, 3, format(false)); addHeader(cursor); QFontMetricsF metrics(font()); qreal headerHeight = metrics.boundingRect(QRectF(), QFont::Bold, "Column 1").height() + 2; // +2 for border auto pageSize = printer->pageRect(QPrinter::DevicePixel).size(); qreal heightLeft = pageSize.height() - 2*headerHeight; // 2* for header and footer QRectF col1Rect(0,0, pageSize.width() * 0.8, fontMetrics().height()); QLocale locale; QTextBlockFormat rightAlign; rightAlign.setAlignment(Qt::AlignVCenter | Qt::AlignRight); for (int r = 1; r < row; r++) { auto text = "Row " + QString::number(r) + " Column " + QString::number(10); heightLeft -= metrics.boundingRect(col1Rect, Qt::AlignVCenter|Qt::AlignLeft, text).height(); if(heightLeft < 0){ //cursor.currentTable()->removeRows(cursor.currentTable()->rows() - 1, 1); cursor.currentTable()->appendRows(1); cursor = cursor.currentTable()->rowStart(cursor); cursor.movePosition(QTextCursor::NextCell); addFooter(cursor); heightLeft = pageSize.height() - 2*headerHeight; cursor.movePosition(QTextCursor::End); cursor.insertTable(1, 3, format()); addHeader(cursor); } cursor.currentTable()->appendRows(1); cursor = cursor.currentTable()->rowStart(cursor); cursor.movePosition(QTextCursor::NextCell); for (int c = 0; c < column; c++) { if(c == 0) cursor.insertText("Row " + QString::number(r) + " Column " + QString::number(c)); else { cursor.setBlockFormat(rightAlign); cursor.insertText(locale.toString(1000)); } cursor.movePosition(QTextCursor::NextCell); } } }
one extra function
addFooter
added. -
This, looks like, works:
All I did is subtracted the
reqHeight
fromheightLeft
infor
as it was generating an additional row.void Window::createDocument(){ int row = 200, column = 3; QTextCursor cursor(document); cursor.insertTable(1, 3, format(false)); addHeader(cursor); QFontMetricsF metrics(font()); qreal headerHeight = metrics.boundingRect(QRectF(0,0,0,0), QFont::Bold, "Column 1").height() + 2.0; // +2 for border auto pageSize = printer->pageRect(QPrinter::DevicePixel).size(); qreal heightLeft = pageSize.height() - 2 * headerHeight; // 2* for header and footer QRectF col1Rect(0,0, pageSize.width() * .8, fontMetrics().height()); QLocale locale; QTextBlockFormat rightAlign; rightAlign.setAlignment(Qt::AlignVCenter | Qt::AlignRight); for (int r = 1; r < row; r++) { QString text; if(r % 2 == 0) text = "Long long Long long Long long Long long Long long Long long Long long Long long Long long text"; else text = "Row " + QString::number(r) + " Column " + QString::number(10); auto reqHeight = metrics.boundingRect(col1Rect, Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text).height(); heightLeft -= reqHeight; if(heightLeft < 0){ cursor.currentTable()->removeRows(cursor.currentTable()->rows()-1, 1); cursor.currentTable()->appendRows(1); cursor = cursor.currentTable()->rowStart(cursor); cursor.movePosition(QTextCursor::NextRow); addFooter(cursor); heightLeft = pageSize.height() - 2*headerHeight - reqHeight; // here's an additional reqHeight subtraction cursor.movePosition(QTextCursor::End); cursor.insertTable(1, 3, format()); addHeader(cursor); } cursor.currentTable()->appendRows(1); cursor = cursor.currentTable()->rowStart(cursor); cursor.movePosition(QTextCursor::NextRow); for (int c = 0; c < column; c++) { if(c == 0) cursor.insertText(text); else { cursor.setBlockFormat(rightAlign); cursor.insertText(locale.toString(1000)); } cursor.movePosition(QTextCursor::NextCell); } } }
BUT why? Why does it work? Why do I have to subtract that height for subsequent pages? This technique probably will make earlier failed attempt of adding header in this way successful.