我有一个包含转义 HTML 字符的 RSS 提要,我想将其显示在文本组件中,并使用 和 修剪多余的elide: Text.ElideRight内容wrapMode: text.WordWrap。
虽然这对于纯文本非常有效,但当我使用时,textFormat: Text.RichText修剪不起作用。
如何才能使修剪工作正常进行,或者如果不可能的话,如何在将 HTML 绑定到文本组件之前轻松对其进行编码?
确实Text不elide支持Text.RichText.
Qt bug 跟踪器上有一个bug,在第一次回复后有一个可能的解决方案,我将其复制并粘贴到此处以便于阅读:
\n\nTextEdit {\n property string htmlText: "<b>"+workingText.text+"</b>"\n text: htmlText\n width: parent.width\n onHtmlTextChanged: {elide();}\n onWidthChanged: elide();//Yes, this will be slow for dynamic resizing and should probably be turned off during animations\n function elide(){//Also, width has to be set, just like elide, or it screws up\n text = realText;\n var end = richText.positionAt(width - 28,0);//28 is width of ellipsis\n if(end != realText.length - 7)//Note that the tags need to be taken care of specially.\n text = realText.substr(0,end + 3) + \'\xe2\x80\xa6\' + \'</b>\';//3 is + <b>\n }\n font.pixelSize: 22\n}\nRun Code Online (Sandbox Code Playgroud)\n