Search code examples
c++cssqthtml-tableqtextdocument

How to use the frame attribute for table in QTextDocument, even though it isn't supported?


I'm trying to create a document using QtTextdocument:: setHtml function. The problem is according to the link below not all attributes are available for to me use from html.

http://doc.qt.io/qt-4.8/richtext-html-subset.html

Here is what I want to do

I have the following html which I want to print to a pdf using (QtTextdocument)

<table width=100% frame='box'>
  <tr align='left'>
    <th>For</th>
    <th>Myself</th>
  </tr>
  <tr align='left'>
    <th>Attention</th>
    <th>Mother</th>
  </table>

The Html produces a table with a simple frame. The problem is the attribute "frame" is not within the supported Html Subset for Qt as indicated by the link here. The table tag is supported but not the attribute frame.

Please note that I had already tried using the attribute "border" and setting it to the value "1|0" but it gave borders around the table cells as well which is not what I want.

Here is the C++ code to do it

QTextDocument *document;
QPrinter printer;
Qstring html="<table width=100% frame='box'><tr align='left'><th>For</th>"

        "<th>Myself</th>"+
      "</tr>"+
      "<tr align='left'>"+
        "<th>Attention</th>"+
        "<th>Mother</th>"+
      "</table>";
document->setHtml(html);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(QMarginsF(15, 15, 15, 15));
printer.setOutputFileName("./Report.pdf");
document->print(&printer);

My problem again

When I check the pdf, the table doesn't have the outside frame I want. Does anyone know a way around this? All I need is a black box around the table.


Solution

  • I solved it in the following manner. The idea is simply applying a black canvas under a white sheet.

    I modified the html to be the following

    <table width=100% style='background-color:#000;'>
    <tr>
    <td style='padding:1px '>
    <table width= 100% style='background-color:#fff;'>
      <tr align='left'>
        <th>For</th>
        <th>Myself</th>
      </tr>
      <tr align='left'>
        <th>Attention</th>
        <th>Mother</th>
      </table>
    </td>
    </tr>
    </table>
    
      

    Now I have a black border of 1px around the content and it is displayed very nicely on the pdf. ENJOY