QML- QML 里的文字展示

一、概述

要在QML中显示文本,就创建一个文本项,并将text属性设置为要显示的文本。有这个 text 属性值的元素对象就能显示该文本。

我们可以在文本项上设置几个属性,以设置整个文本块的样式。包括颜色、字体族、字体大小、粗体和斜体。

标记之类的富文本可用于有选择地为文本项的特定部分设置样式。设置Text::textFormat为Text。StyledText来使用此功能。更多细节请参考 Text 的文档。

二、布局文本

默认情况下,Text将显示单行文本,除非它包含嵌入的换行符。对于要换行,我们可以手动设置wrapMode属性,并指定文本的换行宽度。如果没有显式地设置宽度或高度,读取这些属性将返回文本边界矩形的参数(如果显式地设置了宽度或高度,仍然可以使用paintedWidth和paintedHeight)。记住这些参数后,文本就可以像其他元素一样定位了。

Text还支持使用 html 格式的渲染方式。这就很灵活了,qml 里面我现在好像没有看到这个支持 md 语法的

三、代码示例

在这里插入图片描述

import QtQuick 2.3

  Item {
    
    
      id: root
      width: 480
      height: 320

      Rectangle {
    
    
          color: "#272822"
          width: 480
          height: 320
      }

      Column {
    
    
          spacing: 20

          Text {
    
    
              text: 'I am the very model of a modern major general!'

              // color can be set on the entire element with this property
              color: "yellow"

          }

          Text {
    
    
              // For text to wrap, a width has to be explicitly provided
              width: root.width

              // This setting makes the text wrap at word boundaries when it goes
              // past the width of the Text object
              wrapMode: Text.WordWrap

              // You can use \ to escape quotation marks, or to add new lines (\n).
              //  Use \\ to get a \ in the string
              text: 'I am the very model of a modern major general. I\'ve information \
                    vegetable, animal and mineral. I know the kings of england and I \
                    quote the fights historical; from Marathon to Waterloo in order categorical.'

              // color can be set on the entire element with this property
              color: "white"

          }

          Text {
    
    
              text: 'I am the very model of a modern major general!'

              // color can be set on the entire element with this property
              color: "yellow"

              // font properties can be set effciently on the whole string at once
              font {
    
     family: 'Courier'; pixelSize: 20; italic: true; capitalization: Font.SmallCaps }

          }

          Text {
    
    
              // HTML like markup can also be used
              text: '<font color="white">I am the <b>very</b> model of a modern <i>major general</i>!</font>'

              // This could also be written font { pointSize: 14 }. Both syntaxes are valid.
              font.pointSize: 14

              // StyledText format supports fewer tags, but is more efficient than RichText
              textFormat: Text.StyledText
          }
      }
  }

猜你喜欢

转载自blog.csdn.net/qq_43680827/article/details/130383783
QML
今日推荐