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. Multiline text in QTextDocument table cells
Forum Update on Monday, May 27th 2025

Multiline text in QTextDocument table cells

Scheduled Pinned Locked Moved Solved General and Desktop
qtextdocumentqtextcursorqtexttableform
5 Posts 2 Posters 4.5k Views
  • 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.
  • M Offline
    M Offline
    mdelaere
    wrote on 29 Oct 2016, 18:19 last edited by
    #1

    I want to generate an odt document containing a table. I insert one QString per table cell. Some of these QStrings contain '\n' characters and should be displayed as multiple line text in a single cell.

    I write the data to the odt file with the following code:

    QTextDocument doc;
    QTextCursor cursor(&doc);
    QTextTableFormat tableFormat;
    tableFormat.setCellPadding(5);
    cursor.insertTable(rows, columns, tableFormat);
    
    for (int row = 0; row < rows; ++row)
    {
        for (int col = 0; col < columns; ++col)
        {
            cursor.insertText( someQString );
            cursor.movePosition(QTextCursor::NextCell);
        }
    }
    
    QTextDocumentWriter writer(filename, "ODF");
    writer.write(&doc);
    

    The '\n' characters result in the cursor being moved to the next cell, instead of writing a second line in the same cell.

    I tried replacing the newline characters by unicode paragraph separators (QString::fromUtf16(0x2029)) or replacing the entire table by a html table (with <br> line breaks) via insertHtml(...), without success, multiple line strings get written into multiple table cells.

    How should I properly write a multiline string to a table cell in an odf file?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 29 Oct 2016, 20:10 last edited by
      #2

      Hi and welcome to devnet,

      Can you provide a complete code sample that shows the behavior ?

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

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mdelaere
        wrote on 30 Oct 2016, 08:46 last edited by
        #3

        Thank you for the warm welcome.

        Below is a complete code sample. Apparently I made a mistake in the html version, this one does actually produce the output that I wanted, so my problem is already solved now.

        I still wonder if it's possible in the plain text version, without manually constructing the html table. I guess I'm doing something wrong with the unciode paragraph separator character?

        code:

        #include <QTextCursor>
        #include <QTextDocument>
        #include <QTextTableFormat>
        #include <QTextDocumentWriter>
        
        void writeTextTable(const QString& text, const QString& filename, int rows, int columns)
        {
            QTextDocument doc;
            QTextCursor cursor(&doc);
            QTextTableFormat tableFormat;
            tableFormat.setCellPadding(5);
            cursor.insertTable(rows, columns, tableFormat);
        
            for (int row = 0; row < rows; ++row)
            {
                for (int col = 0; col < columns; ++col)
                {
                    cursor.insertText(text);
                    cursor.movePosition(QTextCursor::NextCell);
                }
            }
        
            QTextDocumentWriter writer(filename, "ODF");
            writer.write(&doc);
        }
        
        void writeParagraphBreak(const QString& text, const QString& filename, int rows, int columns)
        {
            ushort unicodeChar(0x2029);
            QString transformedText(text);
            transformedText.replace('\n', QString::fromUtf16(&unicodeChar));
            writeTextTable(transformedText, filename, rows, columns);
        }
        
        void writeHtmlTable(const QString& text, const QString& filename, int rows, int columns)
        {
            QString htmlText(text);
            htmlText.replace('\n', "<br>");
        
            QTextDocument doc;
            QTextCursor cursor(&doc);
            QString html ("<table>");
        
            for (int row = 0; row < rows; ++row)
            {
                html.append("<tr>");
                for (int col = 0; col < columns; ++col)
                {
                    html.append("<td>");
                    html.append(htmlText);
                    html.append("</td>");
                }
                html.append("</tr>");
            }
        
            html.append("</table>");
            cursor.insertHtml(html);
        
            QTextDocumentWriter writer(filename, "ODF");
            writer.write(&doc);
        }
        
        int main(int argc, char *argv[])
        {
            int rows = 3;
            int columns = 5;
            QString cellText ("This text\nshould be in\na single cell!");
        
            writeTextTable(cellText, "slashN.odt", rows, columns);  // wrong output
            writeParagraphBreak(cellText, "paragraphBreak.odt", rows, columns); // weird output
            writeHtmlTable(cellText, "htmlTable.odt", rows, columns); // produces the desired output
        
            return 0;
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Oct 2016, 23:27 last edited by
          #4

          From the insertText documentation: Any ASCII linefeed characters (\n) in the inserted text are transformed into unicode block separators, corresponding to insertBlock() calls.

          So that likely is the reason of the behavior.

          Note that you can workaround that by replacing the \n with a <br> tag and use cursor.insertFragment(QTextDocumentFragment::fromHtml(text)); in place of insertText.

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

          M 1 Reply Last reply 6 Nov 2016, 21:37
          1
          • S SGaist
            30 Oct 2016, 23:27

            From the insertText documentation: Any ASCII linefeed characters (\n) in the inserted text are transformed into unicode block separators, corresponding to insertBlock() calls.

            So that likely is the reason of the behavior.

            Note that you can workaround that by replacing the \n with a <br> tag and use cursor.insertFragment(QTextDocumentFragment::fromHtml(text)); in place of insertText.

            M Offline
            M Offline
            mdelaere
            wrote on 6 Nov 2016, 21:37 last edited by
            #5

            @SGaist
            Good idea, I hadn't considered that approach.
            Thanks for the help!

            1 Reply Last reply
            1

            2/5

            29 Oct 2016, 20:10

            • Login

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