3
votes

I am working with Qtextdocument to generate a report, I have programmatically prepared a table string as html string, but when I render it the borders are not shown(picture 2)

In browser I get (picture 1) want

but in my Qt App I get (picture 2) get

and here is the string example:

<html>
<head>
<style>
table, th, td { white-space: nowrap; border: 1px solid black; border-collapse: collapse; padding: 2px 4px; text-align: center;}
</style>
</head>
 <body>
  <table width="100%">
  <colgroup span="2" style="border: 2px solid black;">
  <colgroup span="7" style="border: 2px solid black;">
  <colgroup span="7" style="border: 2px solid black;">
  <colgroup span="9" style="border: 2px solid black;">
  <colgroup span="8" style="border: 2px solid black;">
  <colgroup span="1" style="border: 2px solid black;">
    <tr> 
     <td colspan="2" > <img src="Log1.png" width="192" height="86"/></td> 
     <td colspan="31" > Title line1<br/>line2</td> 
     <td> <img src="Log2.png" width="86" height="86" /> </td> 
    </tr>
<tr>
  <td>Month:</td><td>june-19</td><td>01</td><td>02</td><td>03</td><td>04</td><td>05</td><td>06</td><td>07</td><td>08</td><td>09</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td>Total</td>
 </tr>

 <tr>
  <td colspan="2">col span 2</td><td colspan="7">col span 7</td><td colspan="7">col span 7</td><td colspan="9">col span 9</td><td colspan="8">col span 8</td>
 </tr>
 <tr style="border:2px solid black;">
  <td colspan="34">long line sub title</td>
 </tr>
 <tr>
  <td rowspan="3">item1</td>
  <td>item1.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>29</td>
 <tr>
  <td>item1.2</td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>29</td>
 </tr>
 <tr style="border-bottom:2px solid black;">
  <td>item1.3</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>31</td>
 </tr>     
  <tr>
  <td rowspan="2">item2</td>
  <td>item2.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>31</td>
 <tr  style="border-bottom:2px solid black;">
  <td>item2.2</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td>28</td>
 </tr>
 <tr>
  <td> item3</td>
  <td>item3.1</td><td>X</td><td>X</td><td>X</td><td>X</td><td></td><td></td><td></td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>28</td>
 </tr>
</table>
 </body>
</html>
1
Do you have a minimum, reproducible example you could add to your question?technogeek1995
question updatedImla Assuom

1 Answers

1
votes

Instead of using a half-backed approach using QTextDocument and QLineEdit, that seemingly supports just a subset of HTML you should pursue the full-blown approach by using QWebEngineView, if this fits your requirements.

If your requirements are just to create a report, that shouldn't be edited by the user in a QLineEdit you should definitely give QWebEngineView a try. I'm using it to create reports in various apps and the printing option as a PDF is definitely an asset.

The following worked for me and nicely draws the borders:

#include <QApplication>
#include <QWebEngineView>
#include <QIODevice>
#include <QFile>

int main(int argc, char** args) {
    QApplication app(argc, args);
    QFile file("./data/test.html");
    file.open(QIODevice::ReadOnly);
    QString content=QString::fromStdString(file.readAll().toStdString());
    auto webView = new QWebEngineView;
    webView->setHtml(content);
    webView->show();
    QObject::connect(&app, &QApplication::aboutToQuit, [&webView]()
        {
            webView->deleteLater(); // Prevents crash
        });
    app.exec();
}