Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I prevent a QTextTable row being split over page breaks?
Forum Updated to NodeBB v4.3 + New Features

How can I prevent a QTextTable row being split over page breaks?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by l3u_
    #1

    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 several QTextTables, in separate QTextBlocks. 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:
    1.png

    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:
    2.png

    How can I prevent this?

    Thanks for all help!

    SGaistS 1 Reply Last reply
    1
    • l3u_L l3u_

      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 several QTextTables, in separate QTextBlocks. 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:
      1.png

      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:
      2.png

      How can I prevent this?

      Thanks for all help!

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      l3u_L 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by
        #3

        @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 …):
        demo.png

        1 Reply Last reply
        0
        • l3u_L Offline
          l3u_L Offline
          l3u_
          wrote on last edited by
          #4

          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 …

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Deymos
            wrote last edited by
            #5

            I have the same problem, anyone solved this?

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Deymos
              wrote last edited by
              #6

              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...

              D 1 Reply Last reply
              0
              • D Deymos

                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...

                D Offline
                D Offline
                Deymos
                wrote last edited by
                #7

                @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:
                80d737c8-6e88-4296-911e-54edcc6a2e79-image.png
                Here it was page break in the middle of the row and placed the text in the page break

                Probably needed support of

                page-break-inside: avoid
                
                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Deymos
                  wrote last edited by
                  #8

                  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

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved