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

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

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

最佳答案

实际上,Text不支持elide

Qt错误跟踪器上有一个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
}

10-07 15:05