简体   繁体   中英

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

I'm using a HTML table within a QML Text component. My problem is that textFormat: Text.AutoText does not automatically recognize my HTML table as a rich text ( QML Text documentary ).

Searching for a solution I found HTML formatting in QML Text which is quite close to my problem. The solution given: just setting textFormat: Text.RichText I knew before. But I can not use it as setting the textFormat: Text.RichText also changes how the contentWidth of the QML Text component behaves.

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.
Accessing myRichText.contentWidth does always give me 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. Hitting the with limit (eg. 500) for HTML tables would be ok, even so I would prefer knowing the actual table with.

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 . 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.

As you can see, it distiguishes between plain and styled text.
The third category: RichText is not supported by AutoText .

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

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 . Therefore it is not possible to use eg elide together with RichText . The unexpected behavior of contentWidth however seems like a bug to me - in either the source or more likely in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM