1 简述
Text 文本元素可以显示普通文本,也能渲染富文本。支持很多属性,是其他组件显示文本的基础。
常用字体属性:字体名、像素大小、点大小、字宽、字重、粗体、下划线、斜体等
常用文本属性:自动大小、自动换行、显示不全显示省略号等
2 示例
2.1 文本显示
Text {
font.family: "微软雅黑"
text:"打开"
}
3 属性
属性 | 类型 | 描述 |
---|---|---|
advance | size | |
antialiasing | bool | 抗锯齿开关,默认开启 |
baseUrl | url | |
bottomPadding | real | |
clip | bool | 文本裁剪,默认情况Text随字符串长度自动调整宽度;当clip为true,超过Text元素width将不会显示;若clip为false,text则会超出Text区域显示 |
color | color | 文本颜色 |
contentHeight | real | |
contentWidth | real | |
effectiveHorizontalAlignment | enumeration | |
elide | enumeration | 显示设置宽度时,省略过长字符,支持省略号显示在字符串的左、中、右三个位置 |
font.bold | bool | 粗体 |
font.capitalization | enumeration | 设置英文大小写,支持默认、全大写、全小写、分词首字母大写 |
font.family | string | 字体名称 |
font.hintingPreference | enumeration | |
font.italic | bool | 斜体 |
font.kerning | bool | |
font.letterSpacing | real | 字符间距 |
font.pixelSize | int | 字体像素大小 |
font.pointSize | real | 字号大小,与设备无关 |
font.preferShaping | bool | |
font.strikeout | bool | 删除线 |
font.styleName | string | |
font.underline | bool | 下划线 |
font.weight | int | 字重 |
font.wordSpacing | real | 单词间距 |
fontInfo.bold | bool | |
fontInfo.family | string | |
fontInfo.italic | bool | |
fontInfo.pixelSize | string | |
fontInfo.pointSize | real | |
fontInfo.styleName | string | |
fontInfo.weight | int | |
fontSizeMode | enumeration | 字体大小模式 Text.FixedSize(默认)- 使用font.pixelSize或font.pointSize指定的大小。 Text.HorizontalFit - 根据width调整最大字号。 Text.VerticalFit - 根据height使用最大字号。 Text.Fit - 根据宽高调整最大字号 需要使用minimumPointSize或minimumPixelSize指定最小边界,font.pointSize 或 font.pixelSize属性指定的最大边界 |
horizontalAlignment | enumeration | 水平对齐方式 |
hoveredLink | string | 悬浮链接 |
leftPadding | real | |
lineCount | int | 行数,富文本不支持 |
lineHeight | real | |
lineHeightMode | enumeration | |
linkColor | color | |
maximumLineCount | int | |
minimumPixelSize | int | |
minimumPointSize | int | |
padding | real | |
renderType | enumeration | 默认Qt渲染,可选Native渲染,可能出现锯齿 |
renderTypeQuality | int | 渲染质量 ,大号字体需关注 |
rightPadding | real | |
style | enumeration | 文本样式 |
styleColor | color | |
text | string | 文本内容 |
textFormat | enumeration | |
topPadding | real | |
truncated | bool | |
verticalAlignment | enumeration | 垂直对齐方式 |
wrapMode | enumeration | 设置此属性以将文本换行为文本项的宽度。仅当设置了显式宽度时,文本才会换行。包装模式可以是以下之一: Text.NoWrap(默认)- 不会执行换行。如果文本包含的换行符不足,则contentWidth将超过设置的宽度。 Text.WordWrap - 仅在单词边界上进行换行。如果单词太长,contentWidth将超过设置的宽度。 Text.WrapAnywhere - 换行是在一行上的任何一点完成的,即使它发生在单词的中间。 Text.Wrap - 如果可能,在单词边界处进行换行;否则,它将出现在行上的适当位置,甚至在单词的中间。 |
4 中级示例
4.1 超大号字体平滑显示
左右两个Text元素显示83,字体微软雅黑,左边默认属性,右边是设置了字体渲染属性后的效果,可以看到默认属性的Text相当粗糙,Qt5.14没找到可改善此问题的方法,在Qt6.2中查到相关属性。
我们看下属性描述,来自官方6.2的文档
属性:renderTypeQuality : int
覆盖此组件的默认呈现类型质量。这是一种低级自定义,在大多数情况下可以忽略。它目前仅在renderType为Text.QtRendering
时才具有效果。Text.QtRendering使用的栅格化算法可能会生成大文本大小的伪影,例如尖角看起来比它们应该的更圆。如果这是特定文本项的问题,请增加该值以提高呈现质量,但会消耗内存。renderTypeQuality
可以是超过 0 的任何整数,也可以是以下预定义值之一renderTypeQuality
Text.DefaultRenderTypeQuality (default) = -1
Text.LowRenderTypeQuality = 26
Text.NormalRenderTypeQuality = 52
Text.HighRenderTypeQuality = 104
Text.VeryHighRenderTypeQuality = 208
在qt6.2中亲测,这个属性初始化为整数500或以上枚举,无问题。但动态更新此属性会导致程序崩溃
import QtQuick
import QtQuick.Controls 2.0
Item{
Row{
anchors.centerIn: parent
Text {
id:textA
font.family: "微软雅黑"
text:"83"
color: "green"
}
Text {
id:textB
font.family: "微软雅黑"
text:"83"
color: "green"
antialiasing: true //是否使用抗锯齿功能 默认开启
renderType: Text.QtRendering //渲染类型 默认Qt
renderTypeQuality: Text.VeryHighRenderTypeQuality
}
}
Slider{
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
width: parent.width
height: 10
from: 6
to:1600
value:0
onValueChanged: {
textA.font.pixelSize = value
textB.font.pixelSize = value
}
}
}