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 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.
-
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 …
-
I think the whole problem is that the layout engine thinks that the first 2 cells fit into the page, but the last one doesn't, and only partially breaks the row of the table
But in the case, when it inserts characters in the middle of the page break, I don't understand what's wrong.
Now I'm trying to setPageSize to make size of Qtextdocument the same as QPrinter page, taking into account the QPrinter margins (which for some reason cannot be removed or replaced) so that the print preview and TextEdit in the application look identical, and then I will try to run through the document and manually break the table row at the beginning of the row.
I'll let you know if anything comes of it... -
I think the whole problem is that the layout engine thinks that the first 2 cells fit into the page, but the last one doesn't, and only partially breaks the row of the table
But in the case, when it inserts characters in the middle of the page break, I don't understand what's wrong.
Now I'm trying to setPageSize to make size of Qtextdocument the same as QPrinter page, taking into account the QPrinter margins (which for some reason cannot be removed or replaced) so that the print preview and TextEdit in the application look identical, and then I will try to run through the document and manually break the table row at the beginning of the row.
I'll let you know if anything comes of it...@Deymos said in How can I prevent a QTextTable row being split over page breaks?:
I'll let you know if anything comes of it...
no, this is clearly not the right way, another manual iteration through all the blocks takes a long time, and there is no such thing as QTextTableRow to move row to the next page entirely. Instead, there is a QTextTableCell, which apparently calculates when to transfer content, and it transfers only one Cell to the next page, but the nature of such a phenomenon as in the picture is unclear to me, this is clearly a bug:
Here it was page break in the middle of the row and placed the text in the page breakProbably needed support of
page-break-inside: avoid
-
I solved the problem with the text on the page break, in general, you can not put vertical-align:middle or QTextCharFormat::AlignMiddle for the table cell, the engine breaks the cell and thinks that according to the rules, the text should be placed in the middle of the table, and places it on the page break
Most likely a bug