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 does not create a document with nested tables
Qt 6.11 is out! See what's new in the release blog

QTextDocumentWriter does not create a document with nested tables

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 389 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

    I want to get a format file at the output .odt. The file should contain tables, text, and pictures. I started with tables.

    Made a test code:

    ui->textEdit->setHtml(QString(
                                  "<style>"
                                   //для всех таблиц
                                  //"table {width:100%;background: black;border: 2px solid white;}"
                                  //"th { padding: 3px; color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
                                  //"tr { padding: 3px;color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
                                  //"td { padding: 3px;color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
    
                                   //класс на каждую ячейку
                                   ".outer {background-color: cyan;}"
                                   ".inner {padding: 1px; background-color: black;}"
                                   ".cell_A { padding: 3px; color:black; background-color: white;}"
                                   ".cell_B { padding: 3px; color:white; background-color: gray;}"
                                  "</style> "
    
                                "<H1><CENTER>"
                                "<font color='black'><b>ПРОТОКОЛ ИСПЫТАНИЯ АККУМУЛЯТОРА №1</b>"
                                "</CENTER></H1>"
    
                                " <CENTER> "
                                " <TABLE class='outer'>"
                                " <tr>"
    
                                " <td>"
                                " <TABLE class='inner'>"
                                " <tr class='cell_A'><td class='cell_A'>Емкость при разряде</td><td class='cell_A'>43</td></tr>"
                                " <tr class='cell_A'><td class='cell_A'>Остаточная емкость</td><td class='cell_A'>86</td></tr>"
                                " <tr class='cell_A'><td class='cell_A'>Энергия полная</td><td class='cell_A'>556</td></tr>"
                                " <tr class='cell_A'><td class='cell_A'>Емкость при заряде</td><td class='cell_A'>52</td></tr>"
                                " <tr class='cell_A'><td class='cell_A'>КПД заряда</td><td class='cell_A'>83</td></tr>"
                                " </TABLE> "
                                " </td>"
    
                                " <td>"
                                " <TABLE class='inner'>"
                                " <tr class='cell_B'><td class='cell_B'>ЭПС 90% разряд</td><td class='cell_B'>11</td></tr>"
                                " <tr class='cell_B'><td class='cell_B'>ЭПС 50% разряд</td><td class='cell_B'>12</td></tr>"
                                " <tr class='cell_B'><td class='cell_B'>ЭПС 90% заряд</td><td class='cell_B'>13</td></tr>"
                                " <tr class='cell_B'><td class='cell_B'>ЭПС 50% заряд</td><td class='cell_B'>11</td></tr>"
                                " <tr class='cell_B'><td class='cell_B'></td><td class='cell_B'>4</td></tr>"
                                " </TABLE> "
                                " </td>"
    
                                " </tr>"
                                " </TABLE> "
                                " </CENTER>"
                                   ));
    
         textDocument =  ui->textEdit->document();
         QTextDocumentWriter writer;
         writer.setFormat("odf");
         writer.setFileName("/media/usb0/report.odt");
         writer.write(textDocument);
    

    There is no table in the output file. There is a table in QTextEdit, checked by output to the screen.
    что получил.png !
    20230120_165058.jpg

    Pl45m4P 1 Reply Last reply
    0
    • A AlexeyGolubev92

      I want to get a format file at the output .odt. The file should contain tables, text, and pictures. I started with tables.

      Made a test code:

      ui->textEdit->setHtml(QString(
                                    "<style>"
                                     //для всех таблиц
                                    //"table {width:100%;background: black;border: 2px solid white;}"
                                    //"th { padding: 3px; color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
                                    //"tr { padding: 3px;color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
                                    //"td { padding: 3px;color:white; background-color: black;border: 1px solid white;border-collapse:collapse;}"
      
                                     //класс на каждую ячейку
                                     ".outer {background-color: cyan;}"
                                     ".inner {padding: 1px; background-color: black;}"
                                     ".cell_A { padding: 3px; color:black; background-color: white;}"
                                     ".cell_B { padding: 3px; color:white; background-color: gray;}"
                                    "</style> "
      
                                  "<H1><CENTER>"
                                  "<font color='black'><b>ПРОТОКОЛ ИСПЫТАНИЯ АККУМУЛЯТОРА №1</b>"
                                  "</CENTER></H1>"
      
                                  " <CENTER> "
                                  " <TABLE class='outer'>"
                                  " <tr>"
      
                                  " <td>"
                                  " <TABLE class='inner'>"
                                  " <tr class='cell_A'><td class='cell_A'>Емкость при разряде</td><td class='cell_A'>43</td></tr>"
                                  " <tr class='cell_A'><td class='cell_A'>Остаточная емкость</td><td class='cell_A'>86</td></tr>"
                                  " <tr class='cell_A'><td class='cell_A'>Энергия полная</td><td class='cell_A'>556</td></tr>"
                                  " <tr class='cell_A'><td class='cell_A'>Емкость при заряде</td><td class='cell_A'>52</td></tr>"
                                  " <tr class='cell_A'><td class='cell_A'>КПД заряда</td><td class='cell_A'>83</td></tr>"
                                  " </TABLE> "
                                  " </td>"
      
                                  " <td>"
                                  " <TABLE class='inner'>"
                                  " <tr class='cell_B'><td class='cell_B'>ЭПС 90% разряд</td><td class='cell_B'>11</td></tr>"
                                  " <tr class='cell_B'><td class='cell_B'>ЭПС 50% разряд</td><td class='cell_B'>12</td></tr>"
                                  " <tr class='cell_B'><td class='cell_B'>ЭПС 90% заряд</td><td class='cell_B'>13</td></tr>"
                                  " <tr class='cell_B'><td class='cell_B'>ЭПС 50% заряд</td><td class='cell_B'>11</td></tr>"
                                  " <tr class='cell_B'><td class='cell_B'></td><td class='cell_B'>4</td></tr>"
                                  " </TABLE> "
                                  " </td>"
      
                                  " </tr>"
                                  " </TABLE> "
                                  " </CENTER>"
                                     ));
      
           textDocument =  ui->textEdit->document();
           QTextDocumentWriter writer;
           writer.setFormat("odf");
           writer.setFileName("/media/usb0/report.odt");
           writer.write(textDocument);
      

      There is no table in the output file. There is a table in QTextEdit, checked by output to the screen.
      что получил.png !
      20230120_165058.jpg

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @AlexeyGolubev92

      Hi, what Qt version do you use?
      I fear, you cant use <style> tags in QTextEdit / RichText, since in Qt5 LTS and even in the latest Qt6 it doesn't support it. Only the listed tags are usable. That's why your style content is skipped and you only see the empty <tr> table-row.

      • https://doc.qt.io/qt-5/richtext-html-subset.html#supported-tags
      • https://doc.qt.io/qt-6/richtext-html-subset.html#supported-tags

      Edit:
      But maybe it's an issue when writing it to ODF.
      What do you get, when you print your QTextDocument with

      textDocument =  ui->textEdit->document();
      textDocument.toHtml()
      

      https://doc.qt.io/qt-6/qtextdocument.html#toHtml


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      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