Multiline text in QTextDocument table cells
-
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?
-
Hi and welcome to devnet,
Can you provide a complete code sample that shows the behavior ?
-
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; }
-
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 usecursor.insertFragment(QTextDocumentFragment::fromHtml(text));
in place ofinsertText
.