Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML formatting in QML Text

I have a part of HTML code, which displays small table. In browser it looks like in the picture: HTML Table

But when I want to display it in Text QML (which, according to the documentation, should support HTML), I see:

HTML table in QML

(Orange rectangle is a part of rectangle, which is parent of mytext)

Text {
     id: mytext
     anchors.fill: parent
     text: "<div><table border='1'><caption><h4>Test stats</h4>"+
     "</caption><tr bgcolor='#9acd32'><th/><th>Number1</th><th>Number2</th></tr> <tr><th>Line1</th>"+
        "<td> 0 </td> <td> 1 </td> </tr> <tr><th>Line2</th> <td> 0 </td> <td> 1 </td> </tr>"+
        "<tr><th>Line3</th> <td> 0 </td> <td> 0 </td> </tr> <tr><th>Line4</th> <td> 1 </td> <td> 0 </td> </tr>"+
        "<tr><th>Line5</th> <td> 1 </td> <td> 1 </td> </tr> <tr><th>Line6</th> <td> 1 </td> <td> 1 </td> </tr> </div>"
}

So how to display correctly this HTML table in QML (QtQuick 2.0)? Is it possible without using WebView?

like image 797
trivelt Avatar asked Oct 13 '14 06:10

trivelt


People also ask

How to format text in QML?

Displaying and Formatting Text To display text in QML, create a Text item and set the text property to the text you wish to display. The Text item will now display that text. Several properties can be set on the Text item to style the entire block of text. These include color, font family, font size, bold and italic.

Is QML like HTML?

Although QML is not a substitute for HTML – they were designed with different goals in mind – I think QML would make a great web technology. No matter which client framework is currently in fashion, building an UI on top of HTML is using the wrong abstraction for the wrong purpose.

What is difference between Qt and QML?

QML is the language; its JavaScript runtime is the custom V4 engine, since Qt 5.2; and Qt Quick is the 2D scene graph and the UI framework based on it. These are all part of the Qt Declarative module, while the technology is no longer called Qt Declarative.


1 Answers

Add textFormat: Text.RichText:

Text {
     id: mytext
     anchors.fill: parent
     textFormat: Text.RichText
     text: "<div><table border='1'><caption><h4>Test stats</h4>"+
     "</caption><tr bgcolor='#9acd32'><th/><th>Number1</th><th>Number2</th></tr> <tr><th>Line1</th>"+
        "<td> 0 </td> <td> 1 </td> </tr> <tr><th>Line2</th> <td> 0 </td> <td> 1 </td> </tr>"+
        "<tr><th>Line3</th> <td> 0 </td> <td> 0 </td> </tr> <tr><th>Line4</th> <td> 1 </td> <td> 0 </td> </tr>"+
        "<tr><th>Line5</th> <td> 1 </td> <td> 1 </td> </tr> <tr><th>Line6</th> <td> 1 </td> <td> 1 </td> </tr> </div>"
}
like image 155
Meefte Avatar answered Sep 17 '22 09:09

Meefte