【问题标题】:Why does Qt::mightBeRichText() not detact HTML table tags as rich text?为什么 Qt::mightBeRichText() 不能将 HTML 表格标签识别为富文本?
【发布时间】:2017-02-09 15:09:07
【问题描述】:

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

寻找解决方案我发现HTML formatting in QML Text 与我的问题非常接近。 给出的解决方案:只需设置我以前知道的textFormat: Text.RichText。但是我不能用它来设置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
}

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

【问题讨论】:

    标签: html text qml richtext


    【解决方案1】:

    来自Documentation

    如果文本格式为 Text.AutoText,则 Text 项将自动确定文本是否应被视为 样式文本。该确定是使用 Qt::mightBeRichText() 进行的,它使用快速且因此简单的启发式方法。它主要检查在第一个换行符之前是否有看起来像标签的东西。虽然结果对于常见情况可能是正确的,但不能保证。

    如您所见,它区分了 plainstyled 文本。
    第三类:自动图文集不支持富文本

    这意味着对于 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 一起工作。因此无法使用例如elideRichText。然而,contentWidth 的意外行为对我来说似乎是一个错误 - 在源代码中或更可能在文档中。

    【讨论】:

    • 感谢您指出 Qt::mightBeRichText() 确实检查了“mightBeStyledText”
    • 再次提醒您myRichText.implicitWidth。结合Math.min(myRichText.implicitWidth, myRichText.contentWidth) 这个接缝给我我正在寻找的东西
    • 不完全是:考虑一下:Text { text: 'This is so Supercalifragilisticexpialidocious'; width: 200; wrapMode: Text.WordWrap; font.pixelSize: 11 } 对我来说,它的 contentWidth 为 160,implicitWidth 为 208,宽度为 200,因此对于 textFormat: Text.RichText,测量值将减少 40px。尽管如此,除非您要自己逐字计算,否则它很可能是您可以获得的最佳测量值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多