HTML formatting in QML Text

こ雲淡風輕ζ 提交于 2020-01-01 08:47:06

问题


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

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

(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?


回答1:


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>"
}


来源:https://stackoverflow.com/questions/26333867/html-formatting-in-qml-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!