为什么 Qt::mightBeRichText() 没有将 HTML table 标签检测为富文本?

Why does Qt::mightBeRichText() not detact HTML table tags as rich text?

我在 QML 文本组件中使用 HTML table。我的问题是 textFormat: Text.AutoText 不会自动将我的 HTML table 识别为富文本 (QML Text documentary)。

在搜索解决方案时,我发现 HTML formatting in QML Text 与我的问题非常接近。 给出的解决方案:设置textFormat: Text.RichText我之前就知道了。但是我不能使用它,因为设置 textFormat: Text.RichText 也会改变 QML 文本组件的 contentWidth 的行为方式。

Text {
  id: myPlainText
  width: 500
  wrapMode: Text.Wrap
  text: "Hallo whosebug.com"

  textFormat: Text.AutoText
}

Text {
  id: myRichText
  width: 500
  wrapMode: Text.Wrap
  text: "Hallo whosebug.com"      

  textFormat: Text.RichText
}

访问 myPlainText.contentWidth 会给我实际使用的文本,即使它短于 500。
访问 myRichText.contentWidth 总是给我 500。
对我来说,实际使用的信息(当不涉及 RichText 时为 contentWidth)对于布局原因很重要,因为这是我的组件主要用于的内容。达到 HTML tables 的限制(例如 500)是可以的,即便如此我更愿意知道实际的 table with.

来自Documentation

If the text format is Text.AutoText the Text item will automatically determine whether the text should be treated as styled text. This determination is made using Qt::mightBeRichText() which uses a fast and therefore simple heuristic. 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.

如您所见,它区分了 普通样式化 文本。
第三类:RichText不被AutoText支持。

这意味着对于 AutoText,您需要求助于减少的标签集,参见文档:

<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
&gt; &lt; &amp;

如果您需要文本的宽度,请尝试使用

myRichText.implicitWidth

如果没有换行,这将为您提供文本的宽度。
很可能,由于先进的可能性,它总是以最大值 contentWidth 工作。因此不可能使用例如elide 连同 RichText。然而,contentWidth 的意外行为对我来说似乎是一个错误 - 在源代码中或更可能在文档中。