【发布时间】:2015-04-28 23:31:51
【问题描述】:
我有一个带有转义 HTML 字符的 RSS 提要,我想在 Text 组件中显示它,我用 elide: Text.ElideRight 和 wrapMode: text.WordWrap 修剪多余的内容。
虽然这对纯文本非常有效,但当我使用 textFormat: Text.RichText 时,修剪不起作用。
如何使修剪工作,或者,如果不可能,在将 HTML 绑定到文本组件之前轻松编码它?
【问题讨论】:
我有一个带有转义 HTML 字符的 RSS 提要,我想在 Text 组件中显示它,我用 elide: Text.ElideRight 和 wrapMode: text.WordWrap 修剪多余的内容。
虽然这对纯文本非常有效,但当我使用 textFormat: Text.RichText 时,修剪不起作用。
如何使修剪工作,或者,如果不可能,在将 HTML 绑定到文本组件之前轻松编码它?
【问题讨论】:
确实Text 不支持elide 为Text.RichText。
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
}
【讨论】:
我想出了一个简单的解决方法,在我的情况下效果很好,其中 Text 组件的宽度和高度是固定的,即手动修剪字符串。最终结果在一定程度上取决于您的字体的等宽。
text: {
var name = "my very very long text string with HTML markup that goes on forever and ever and ever"
var text = name.substring(0,45)
if (name.length > 45) text += "..."
return text
}
【讨论】: