garlix
garlix

Reputation: 596

QTextDocument HTML Table width

I'm struggling with a QT5 problem. I'm trying to build (and then paint on a QPainter object) an HTML table. This is my code:

QTextDocument td;
QString table_style= QString("<style type=\"text/css\">"
                             ".tg  { table-layout: fixed; width: 200px; }"
                             ".tg td{padding-bottom: 5px;border-style:solid;border-width:0px;}"
                             ".tg .tg-3x1q{color: rgba(255, 255, 255, 0.5); text-align:right}"
                             ".tg .tg-6bqv{color: rgba(255, 255, 255, 0.5); padding-left: 5px;}"
                             "</style>");

QString table_html = QString(" <table width=\"500px\" class=\"tg\">"
                             "<tr>"
                               "<td class=\"tg-3x1q\" width=\"150px\">1</td>"
                               "<td class=\"tg-6bqv\" width=\"150px\"><sup>th</sup></td>"
                             "</tr>"
                             "<tr>"
                               "<td class=\"tg-3x1q\" width=\"150px\">2</td>"
                               "<td class=\"tg-6bqv\" width=\"150px\"><sup>rpm</sup></td>"
                             "</tr>"
                             "<tr>"
                               "<td class=\"tg-3x1q\" width=\"150px\">3</td>"                   
                               "<td class=\"tg-6bqv\" width=\"150px\"><sup>km/h</sup></td>"
                             "</tr>"
                             "</table>");

td.setDefaultStyleSheet(table_style);
td.setHtml(table_html);
td.drawContents(painter);

The table is correctly showed in the painter BUT has no fixed column width. To achieve that result I've tried almost everything:

None of these ways worked. Moreover, if I try to add some spaces to pad the row they were trimmed out once the table was painted. I've also read that I could use the WebView to achieve the result but it's too slow. I'm stuck, I hope that someone could help me to figure it out.

Upvotes: 2

Views: 4683

Answers (1)

QTextDocument is not a renderer of any standard HTML. It implements a slightly incompatible HTML- and CSS-subset. For example, the px suffix is not supported. All dimensions are either in pixels (without a suffix) or in percent (with % suffix). You might wish to try the widths without the px suffix.

You can only rely on QTextDocument-related documentation of the markup it implements, not on general HTML/CSS documentation.

Upvotes: 7

Related Questions