[英]Why does Qt::mightBeRichText() not detact HTML table tags as rich text?
I'm using a HTML table within a QML Text component. 我在QML文本组件中使用HTML表。 My problem is that textFormat: Text.AutoText
does not automatically recognize my HTML table as a rich text ( QML Text documentary ). 我的问题是textFormat: Text.AutoText
无法自动将我的HTML表识别为富文本( QML Text documentary )。
Searching for a solution I found HTML formatting in QML Text which is quite close to my problem. 在寻找解决方案时,我发现QML Text中的HTML格式非常接近我的问题。 The solution given: just setting textFormat: Text.RichText
I knew before. 给定的解决方案:只需设置textFormat: Text.RichText
我以前知道的textFormat: Text.RichText
。 But I can not use it as setting the textFormat: Text.RichText
also changes how the contentWidth
of the QML Text component behaves. 但是我不能使用它来设置textFormat: Text.RichText
还会更改QML Text组件的contentWidth
行为。
Text {
id: myPlainText
width: 500
wrapMode: Text.Wrap
text: "Hallo stackoverflow.com"
textFormat: Text.AutoText
}
Text {
id: myRichText
width: 500
wrapMode: Text.Wrap
text: "Hallo stackoverflow.com"
textFormat: Text.RichText
}
Accessing myPlainText.contentWidth
will give me the actual used with of the text even if it is shorter than 500. 访问myPlainText.contentWidth
将为我提供实际使用的文本,即使该文本短于500。
Accessing myRichText.contentWidth
does always give me 500. 访问myRichText.contentWidth
确实总是给我500。
For me the information of the actual used with, which is contentWidth
when no RichText
is involved, is important for layout reasons, as this is what my component is mostly used for. 对我来说,实际使用的信息(不涉及RichText
时为contentWidth
由于布局原因很重要,因为这是我的组件最常使用的信息。 Hitting the with limit (eg. 500) for HTML tables would be ok, even so I would prefer knowing the actual table with. 达到HTML表格的with限制(例如500)是可以的,即使这样我也希望了解实际的表格。
From the Documentation 从文档中
If the text format is Text.AutoText the Text item will automatically determine whether the text should be treated as styled text . 如果文本格式为Text.AutoText,则文本项将自动确定是否应将文本视为样式文本 。 This determination is made using Qt::mightBeRichText() which uses a fast and therefore simple heuristic. 该确定是使用Qt :: mightBeRichText()进行的,该方法使用了快速且简单的启发式方法。 It mainly checks whether there is something that looks like a tag before the first line break. 它主要检查在第一个换行符之前是否有一些看起来像标签的东西。 Although the result may be correct for common cases, there is no guarantee. 尽管在某些情况下结果可能是正确的,但不能保证。
As you can see, it distiguishes between plain and styled text. 如您所见,它在纯文本和样式文本之间有区别。
The third category: RichText is not supported by AutoText . 第三类: AutoText不支持RichText 。
This means for AutoText you need to resort to the reduced set of tags, seen in the documentation: 这意味着对于自动图文集,您需要诉诸简化的标签集,如文档所示:
<b></b> - bold
<strong></strong> - bold
<i></i> - italic
<br> - new line
<p> - paragraph
<u> - underlined text
<font color="color_name" size="1-7"></font>
<h1> to <h6> - headers
<a href=""> - anchor
<img src="" align="top,middle,bottom" width="" height=""> - inline images
<ol type="">, <ul type=""> and <li> - ordered and unordered lists
<pre></pre> - preformatted
> < &
If you need the width of your text, try to use 如果您需要文本的宽度,请尝试使用
myRichText.implicitWidth
This will give you the width of the text, if it is not wrapped. 如果没有包装,这将为您提供文本的宽度。
Propbably, due to the advanced posibilities, it always works with a maximum contentWidth
. 可能由于先进的可能性,它始终在最大contentWidth
。 Therefore it is not possible to use eg elide
together with RichText
. 因此,不可能将例如elide
与RichText
一起使用。 The unexpected behavior of contentWidth
however seems like a bug to me - in either the source or more likely in the documentation. 但是, contentWidth
的意外行为对我来说似乎是个错误-在源代码中或更可能在文档中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.