Qml:使用Text的elide属性和textFormat:RichText

时间:2015-04-28 13:47:39

标签: qt qml

我有一个带有转义HTML字符的RSS Feed,我想在Text组件中显示,我用elide: Text.ElideRightwrapMode: text.WordWrap修剪多余的内容。

虽然这对纯文本非常有效,但当我使用textFormat: Text.RichText时,剪裁不起作用。

如何使修剪工作,或者如果不可能,在将HTML绑定到文本组件之前轻松编码HTML?

2 个答案:

答案 0 :(得分:2)

Text确实elide不支持Text.RichText

Qt bug跟踪器上有一个bug open,在第一次回复之后有一个可能的解决方案,我可以复制并粘贴到这里以便于阅读:

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
}

答案 1 :(得分:0)

我想到了一个简单的解决方法,在我的情况下效果很好,其中Text组件的宽度和高度是固定的,即手动修剪字符串。最终结果取决于你的字体是如何等宽的。

text:  {
    var name = "my very very long text string with HTML markup&nbsp;that goes on forever and ever and ever"
    var text = name.substring(0,45)
    if (name.length > 45) text += "..."
        return text
}