属性
advance : sizeantialiasing : boolbaseUrl : urlclip : boolcolor : color//字符内容宽高度contentHeight : realcontentWidth : realeffectiveHorizontalAlignment : enumeration//字符超过显示长度省略显示elide : enumerationText.ElideNone - the defaultText.ElideLeftText.ElideMiddleText.ElideRight//字体参数的设置font.bold : bool 粗体font.capitalization : enumerationfont.family : string 字体类型font.hintingPreference : enumerationfont.italic : bool 斜体font.kerning : boolfont.letterSpacing : real 字母与字母之间的距离font.pixelSize : int 字体大小 像素做单位font.pointSize : real 字体大小 磅做单位font.preferShaping : boolfont.strikeout : boolfont.styleName : stringfont.underline : bool 下划线font.weight : enumerationfont.wordSpacing : realfontInfo.bold : boolfontInfo.family : stringfontInfo.italic : boolfontInfo.pixelSize : stringfontInfo.pointSize : realfontInfo.styleName : stringfontInfo.weight : intfontSizeMode : enumerationhorizontalAlignment : enumerationhoveredLink : stringlineCount : int 文本行数lineHeight : real 每行间隔高度lineHeightMode : enumerationlinkColor : colormaximumLineCount : intminimumPixelSize : intminimumPointSize : intrenderType : enumerationstyle : enumerationstyleColor : colortext : stringtextFormat : enumeration//字体距离边框顶部距离topPadding : realpadding : real//体距离边框左侧距离leftPadding : real//体距离边框底部距离bottomPadding : real//体距离边框右侧距离rightPadding : realtruncated : boolverticalAlignment : enumerationwrapMode : enumeration //换行方式 文本换行Text.WordWrap
信号
lineLaidOut(object line)linkActivated(string link)linkHovered(string link)
方法
forceLayout()linkAt(real x, real y)
显示文本你需要使用Text元素(Text Element)。它最值得注意的属性时字符串类型的text属性。这个元素会使用给出的text(文本)与font(字体)来计算初始化的宽度与高度。可以使用字体属性组来(font property group)来改变当前的字体,例如font.family,font.pixelSize,等等。改变文本的颜色值只需要改变颜色属性就可以了。
样式
Column {Rectangle{width: 100height: txt.contentHeightopacity: 0.5color: "blue"Text {id: txtanchors.fill: parenttext: "Hello World! 152285242151"font.family: "Helvetica"font.pointSize: 20elide: Text.ElideRightcolor: "red"leftPadding: 0// padding: 20// topPadding: 20// bottomPadding: 20}}Text { font.pointSize: 24; text: "Normal" }Text { font.pointSize: 24; text: "Raised"; style: Text.Raised; styleColor: "#AAAAAA" }Text { font.pointSize: 24; text: "Outline";style: Text.Outline; styleColor: "red" }Text { font.pointSize: 24; text: "Sunken"; style: Text.Sunken; styleColor: "#AAAAAA" }} Text {text: "The quick brown fox"color: "#303030"font.family: "Ubuntu"font.pixelSize: 28}

文本可以使用horizontalAlignment与verticalAlignment属性来设置它的对齐效果。为了提高文本的渲染效果,你可以使用style和styleColor属性来配置文字的外框效果,浮雕效果或者凹陷效果。对于过长的文本,你可能需要使用省略号来表示,例如A very … long text,你可以使用elide属性来完成这个操作。elide属性允许你设置文本左边,右边或者中间的省略位置。如果你不想’….’省略号出现,并且希望使用文字换行的方式显示所有的文本,你可以使用wrapMode属性(这个属性只在明确设置了宽度后才生效):
Text {width: 40; height: 120text: 'A very long text'// '...' shall appear in the middleelide: Text.ElideMiddle //省略。。。替换过长文本// red sunken text stylingstyle: Text.SunkenstyleColor: '#FF4444'// align text to the topverticalAlignment: Text.AlignTop// only sensible when no elide mode// wrapMode: Text.WordWrap //换行}
一个text元素(text element)只显示的文本,它不会渲染任何背景修饰。除了显示的文本,text元素背景是透明的。为一个文本元素提供背景是你自己需要考虑的问题。
文本内容格式的支持
Column {Text {font.pointSize: 24text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.RichText //富文本text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.PlainText //纯文本text: "<b>Hello</b> <i>World!</i>"}Text {font.pointSize: 24textFormat: Text.MarkdownTexttext: "**Hello** *World!*"}}

注意
知道一个文本元素(Text Element)的初始宽度与高度是依赖于文本字符串和设置的字体这一点很重要。一个没有设置宽度或者文本的文本元素(Text Element)将不可见,默认的初始宽度是0。
显示超链接:
Rectangle{id: rectwidth: 200height: 50border.color: "black"Text {// textFormat: Text.RichTexttext: "See the <a href=\"http://qt-project.org\">Qt Project website</a>."MouseArea{anchors.fill: parenthoverEnabled: truecursorShape: Qt.PointingHandCursor //修改鼠标移动到文本上是图案onClicked: {//鼠标点击需要做的操作}}onLinkActivated: { //点击链接触发console.log(link + " link activated")}onLinkHovered: { //鼠标移动到文本上触发console.log("hover ",link)}onHoveredLinkChanged: {console.log("hover link changed: ",link)}}}

注意
通常你想要对文本元素布局时,你需要区分文本在文本元素内部的边界对齐和由元素边界自动对齐。前一种情况你需要使用horizontalAlignment和verticalAlignment属性来完成,后一种情况你需要操作元素的几何形状或者使用anchors(锚定)来完成。
