How can I prevent a QTextTable row being split over page breaks?
-
Hi all!
I'm implementing a small program to print lists of items with QR codes containing item-specific data. The following happens both when using Qt 5.15.8 and 6.4.2.
I use a
QTextDocument
and severalQTextTable
s, in separateQTextBlock
s. This is the first time I mess with printing, so maybe, It's some beginner issue. However, I have no idea why the following happens or how to prevent it:At a specific size of the QR codes, everything is fine. The printed PDF looks like it should:
But as soon as I slightly increase the size of the QR codes, rows at the end of a page are split and appear on two pages. Also the text is displaced:
How can I prevent this?
Thanks for all help!
-
Hi,
Can you provide a minimal compilable example that shows this behaviour ?
In the old days, I remember implementing the tables using html and doing checks to learn when the split would happen and ensure rows would break at the right time.
-
@SGaist Hi :-)
The following minimal example produces a split up row:
#include <QApplication> #include <QImage> #include <QPrinter> #include <QTextDocument> #include <QTextCursor> #include <QTextTableFormat> #include <QTextImageFormat> int main(int argc, char *argv[]) { QApplication app(argc, argv); QPrinter printer(QPrinter::HighResolution); printer.setPageSize(QPageSize::A4); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(QStringLiteral("/tmp/demo.pdf")); QTextDocument document; QTextCursor cursor(&document); QTextTableFormat tableFormat; tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100)); tableFormat.setCellPadding(10); tableFormat.setBorderCollapse(true); tableFormat.setBorderBrush(QBrush(Qt::SolidPattern)); cursor.insertTable(16, 3, tableFormat); for (int i = 0; i < 16; i++) { cursor.insertText(QStringLiteral("Some text")); cursor.movePosition(QTextCursor::NextCell); cursor.insertText(QStringLiteral("Some price")); cursor.movePosition(QTextCursor::NextCell); QImage image(57, 57, QImage::Format_RGB32); image.fill(qRgb(0, 0, 0)); const auto url = QStringLiteral("mydata://%1.png").arg(i); document.addResource(QTextDocument::ImageResource, QUrl(url), QVariant(image)); QTextImageFormat pictureFormat; pictureFormat.setName(url); pictureFormat.setHeight(90); pictureFormat.setWidth(90); cursor.insertImage(pictureFormat); cursor.movePosition(QTextCursor::NextCell); } document.print(&printer); return app.exec(); }
The output
/tmp/demo.pdf
renders als follows (at least here …):
-
Okay. While it still would be interesting how to fix this:
For my use-case, it actually seems to be easier to generate a HTML file (HTML 5 with full CSS) and let a browser print it. This way, the browser has to mess with all the page layouting, and the result looks good. Also creating it seems to be easier …