Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved create .odt with headings and tables

    General and Desktop
    3
    5
    890
    Loading More Posts
    • 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.
    • mastupristi
      mastupristi last edited by mastupristi

      I need to create a .odt file that contains headings (such as heading 1 or heading 2) and tables.

      I tried something like this:

      #include <QString>
      #include <QTextStream>
      #include <QTextDocument>
      #include <QTextDocumentWriter>
      
      int main(void)
      {
          QString strStream;
          QTextStream out(&strStream);
      
          QTextDocument *document = new QTextDocument();
      
          out << "<!DOCTYPE html>"
              << "<html>\n"
              << "<head>"
              << "<title>Test</title>"
              << "</head>"
              << "<body>"
              << "<h1>Test heading 1</h1>"
              << "</body>"
              << "</html>";
          document->setHtml(strStream);
      
          QTextDocumentWriter writer("test.odt");
      
          writer.setFormat("odf");
          writer.write(document);
      
          delete document;
          return 0;
      }
      

      but the product .odt file has no heading.
      screenshot
      what I would like to be heading 1 doesn't have the Heading 1 style, but, in fact, it's a Default Style bold text.

      How do I force Heading 1 style?

      best regards
      Max

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

        This is not a problem of QTextDocumentWriter, the information about the h1 tag is lost in document->setHtml(strStream); (ref1 and ref2).

        Fixing this would imply a huge effort.

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        mastupristi 1 Reply Last reply Reply Quote 2
        • mastupristi
          mastupristi @VRonin last edited by

          @VRonin Is there an alternative way to html to get an odt with heading 1 and a table, maybe even with custom styles?

          Gojir4 1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            The easiest method I see is using QXmlStreamWriter + a zip library to create the file from scratch but, as you can imagine, it's far from being "easy"

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply Reply Quote 2
            • Gojir4
              Gojir4 @mastupristi last edited by

              @mastupristi said in create .odt with headings and tables:

              @VRonin Is there an alternative way to html to get an odt with heading 1 and a table, maybe even with custom styles?

              You can create you own style using QTextCharFormat and QTextBlockFormat and then creating the document using the QTextDocument API.

              Here is an example:
              You will see htmlToOdf() function is simply your code, but createsOdf() and createsOdfWithTable() creates a ODT file using custom style the the QTextDocument API. Hope it helps

              #include <QString>
              #include <QTextStream>
              #include <QTextDocument>
              #include <QTextDocumentWriter>
              #include <QTextCursor>
              #include <QTextBlock>
              #include <QDate>
              #include <QTextTableCell>
              
              
              void htmlToOdf(){
                  QString strStream;
                  QTextStream out(&strStream);
              
                  QTextDocument *document = new QTextDocument();
              
                  out << "<!DOCTYPE html>"
                      << "<html>"
                      << "<head>"
                      << "<title>Test</title>"
                      << "</head>"
                      << "<body>"
                      << "<h1>Test heading 1</h1>"
                      << "</body>"
                      << "</html>";
                  document->setHtml(strStream);
              
                  QTextDocumentWriter writer("test.odt");
                  writer.setFormat("odf");
                  writer.write(document);
                  QTextDocumentWriter writer2("test.html");
                  writer2.setFormat("html");
                  writer2.write(document);
              
                  delete document;
              }
              
              void createsOdf(){
                  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 titleFormat = boldFormat;
                  titleFormat.setFontPointSize(20);
                  header1Format.setForeground(QBrush(QColor("#0000FF")));
              
                  QTextBlockFormat titleBlockFormat = cursor.block().blockFormat();
                  titleBlockFormat.setAlignment(Qt::AlignHCenter);
                  QTextBlockFormat blockFormat = cursor.block().blockFormat();
              
              
                  //Title of the document
                  cursor.insertBlock();
                  cursor.setBlockCharFormat(titleFormat);
                  cursor.setBlockFormat(titleBlockFormat);
                  cursor.insertText(QObject::tr("Test !"));
                  cursor.insertBlock(); //new line
              
                  cursor.setBlockFormat(blockFormat);
                  cursor.insertText(QObject::tr("Header 1"), header1Format);
                  cursor.insertBlock();
              
                  cursor.movePosition(QTextCursor::End);
              
                  QTextDocumentWriter writer("test2.odt");
                  writer.setFormat("odf");
                  writer.write(doc);
                  QTextDocumentWriter writer2("test2.html");
                  writer2.setFormat("html");
                  writer2.write(doc);
              
                  delete doc;
              }
              
              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.setBorderBrush(QBrush(QColor("#0072A6")));
                  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 " + 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("test3.html");
                  writer2.setFormat("html");
                  writer2.write(doc);
              
                  delete doc;
              
              }
              
              int main(void)
              {
                  htmlToOdf();
                  createsOdf();
                  createsOdfWithTable();
              
                  return 0;
              }
              
              
              1 Reply Last reply Reply Quote 2
              • First post
                Last post