Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qml: use Text's elide property with textFormat: RichText

Tags:

qt

qml

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.

While this works very well for plain text, when I use textFormat: Text.RichText the trimming does not work.

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?

like image 608
Marco Piccolino Avatar asked Apr 28 '15 13:04

Marco Piccolino


1 Answers

Indeed Text doesn't support elide for Text.RichText.

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
}
like image 143
rpadovani Avatar answered Oct 20 '22 17:10

rpadovani