问题描述
我有一个带有转义 HTML 字符的 RSS 提要,我想在 Text 组件中显示该提要,在其中我使用 elide: Text.ElideRight
和 wrapMode: text.WordWrap.
I have an RSS feed with escaped HTML characters that I want to display in a Text component where I trim the excess content with elide: Text.ElideRight
and wrapMode: text.WordWrap
.
虽然这对纯文本非常有效,但当我使用 textFormat: Text.RichText
时,修剪不起作用.
While this works very well for plain text, when I use textFormat: Text.RichText
the trimming does not work.
我怎样才能使修剪工作,或者,如果这是不可能的,在将 HTML 绑定到文本组件之前轻松编码它?
How can I make the trimming to work or, if this is impossible, encode the HTML easily prior to binding it to the text component?
推荐答案
确实 Text
不支持 Text.RichText
的 elide
.
Indeed Text
doesn't support elide
for Text.RichText
.
Qt bug tracker上有一个bug open,第一次回复后有一个可能的解决方案,我在这里复制并粘贴以方便阅读:
There is a bug open on the Qt bug tracker, and after the first reply there is a possible solution, that I copy and paste here for an easy read:
TextEdit {
property string htmlText: "<b>"+workingText.text+"</b>"
text: htmlText
width: parent.width
onHtmlTextChanged: {elide();}
onWidthChanged: elide();//Yes, this will be slow for dynamic resizing and should probably be turned off during animations
function elide(){//Also, width has to be set, just like elide, or it screws up
text = realText;
var end = richText.positionAt(width - 28,0);//28 is width of ellipsis
if(end != realText.length - 7)//Note that the tags need to be taken care of specially.
text = realText.substr(0,end + 3) + '…' + '</b>';//3 is + <b>
}
font.pixelSize: 22
}
这篇关于Qml:使用 Text 的 elide 属性和 textFormat:RichText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!