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. QTextDocumentWriter removes carriage return translation to tables when creating a document
Qt 6.11 is out! See what's new in the release blog

QTextDocumentWriter removes carriage return translation to tables when creating a document

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 367 Views 1 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.
  • A Offline
    A Offline
    AlexeyGolubev92
    wrote on last edited by
    #1

    When I create a document using QTextDocumentWriter in the "odf" format, line wrapping in the table does not work. When I create an "html" document, line wrapping in the table works. The "odf" format is required for older versions of the open office. In older versions, the office version "HTML" does not open. How to make a row transfer to a table in "odf" format? Attached the test code:

    #include <QString>
    #include <QTextStream>
    #include <QTextDocument>
    #include <QTextDocumentWriter>
    #include <QTextCursor>
    #include <QTextBlock>
    #include <QDate>
    #include <QTextTableCell>
    
    void createsOdfWithTable(){
    QTextDocument *doc = new QTextDocument;
    doc->setDocumentMargin(10);
    
    QTextCursor cursor(doc);
    cursor.movePosition(QTextCursor::Start);
    
    QTextCharFormat textFormat;
    textFormat.setFontPointSize(10);
    QTextCharFormat boldFormat;
    boldFormat.setFontWeight(QFont::Bold);
    
    //HEADERS
    QTextCharFormat header1Format = boldFormat;
    header1Format.setFontPointSize(12);
    header1Format.setFontUnderline(true);
    header1Format.setForeground(QBrush(QColor("#0000FF")));
    
    QTextCharFormat header2Format = header1Format;
    header2Format.setFontPointSize(14);
    QTextCharFormat header3Format = header1Format;
    header3Format.setFontPointSize(16);
    
    QTextCharFormat titleFormat = boldFormat;
    titleFormat.setFontPointSize(20);
    header1Format.setForeground(QBrush(QColor("#0000FF")));
    
    QTextBlockFormat blockFormat = cursor.block().blockFormat();
    QTextBlockFormat titleBlockFormat = cursor.block().blockFormat();
    titleBlockFormat.setAlignment(Qt::AlignHCenter);
    
    QTextCharFormat alternateCellFormat = textFormat;
    alternateCellFormat.setBackground(QBrush(QColor("#D1EBFF")));
    
    QTextTableFormat tableFormat;
    tableFormat.setBorder(1);
    tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
    tableFormat.setCellPadding(2);
    tableFormat.setCellSpacing(0);
    tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 80));
    tableFormat.setAlignment(Qt::AlignLeft);
    
    //Title of the document
    cursor.insertBlock();
    cursor.setBlockCharFormat(titleFormat);
    cursor.setBlockFormat(titleBlockFormat);
    cursor.insertText(QObject::tr("My Great Document !"));
    cursor.insertBlock(); //new line
    cursor.insertBlock();
    cursor.insertBlock();
    
    cursor.setBlockFormat(blockFormat);
    cursor.insertText(QObject::tr("Header 1"), header1Format);
    cursor.insertBlock();
    
    cursor.setCharFormat(textFormat);
    
    cursor.insertText("Author:\tMe");
    cursor.insertBlock();
    cursor.insertText("Version:\tMe");
    cursor.insertBlock();
    cursor.insertText("Date:\t" + QDate::currentDate().toString("dd.MM.yyyy"));
    cursor.insertBlock();
    
    cursor.insertText(QObject::tr("Header 2"), header2Format);
    cursor.insertBlock();
    cursor.insertText(QObject::tr("Header 3"), header3Format);
    cursor.insertBlock();
    
    //Insert table with nb row = 10, column = 2
    QTextTable *table = cursor.insertTable(10+1, 2, tableFormat);
    QTextTableCell headerCell = table->cellAt(0, 0);
    QTextCursor headerCellCursor = headerCell.firstCursorPosition();
    headerCellCursor.insertText(QObject::tr("Name"), boldFormat);
    headerCell = table->cellAt(0, 1);
    headerCellCursor = headerCell.firstCursorPosition();
    headerCellCursor.insertText(QObject::tr("Value"), boldFormat);
    
    for(int i = 0; i < 10; i++){
        QTextCharFormat cellFormat = i % 2 == 0 ? textFormat : alternateCellFormat;
        QTextTableCell cell = table->cellAt(i + 1, 0);
        cell.setFormat(cellFormat);
        QTextCursor cellCursor = cell.firstCursorPosition();
        cellCursor.insertText("Row \n" + QString::number(i));
    
        cell = table->cellAt(i + 1, 1);
        cell.setFormat(cellFormat);
        cellCursor = cell.firstCursorPosition();
        cellCursor.insertText(QString::number(i));
    }
    
    cursor.movePosition(QTextCursor::End);
    
    
    QTextDocumentWriter writer("test3.odt");
    writer.setFormat("odf");
    writer.write(doc);
    QTextDocumentWriter writer2("test4.odt");
    writer2.setFormat("html");
    writer2.write(doc);
    
    delete doc;
    }
    
    
    int main(int argc, char *argv[])
    {
     QApplication a(argc, argv);
     createsOdfWithTable();
     return a.exec();
    }
    

    odf.png
    html.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You should add which version of Qt you are using as well as which version of LibreOffice.

      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
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @AlexeyGolubev92 said in QTextDocumentWriter removes carriage return translation to tables when creating a document:

        QTextCursor cellCursor = cell.firstCursorPosition();
        cellCursor.insertText("Row \n" + QString::number(i));
        

        Perhaps this (like your earlier code that starts a new paragraph):

            QTextCursor cellCursor = cell.firstCursorPosition();
            cellCursor.insertText("Row");
            cellCursort.insertBlock();
            cellCursor.insertText( QString::number(i));
        
        A 1 Reply Last reply
        0
        • C ChrisW67

          @AlexeyGolubev92 said in QTextDocumentWriter removes carriage return translation to tables when creating a document:

          QTextCursor cellCursor = cell.firstCursorPosition();
          cellCursor.insertText("Row \n" + QString::number(i));
          

          Perhaps this (like your earlier code that starts a new paragraph):

              QTextCursor cellCursor = cell.firstCursorPosition();
              cellCursor.insertText("Row");
              cellCursort.insertBlock();
              cellCursor.insertText( QString::number(i));
          
          A Offline
          A Offline
          AlexeyGolubev92
          wrote on last edited by
          #4

          @ChrisW67 Experienced. The code doesn't work. It does not transfer the string.

          1 Reply Last reply
          0

          • Login

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