本页内容

TextMetrics QML Type

提供给定字体和文本的相关指标。更多...

Import Statement: import QtQuick

属性

详细说明

TextMetrics 用于计算给定文本字符串在特定字体下的各种属性。

它为QFontMetricsF 中那些接受参数的函数提供了一个声明式 API。

TextMetrics {
    id: textMetrics
    font.family: "Arial"
    elide: Text.ElideMiddle
    elideWidth: 100
    text: "Hello World"
}

MyItem {
    text: textMetrics.elidedText
}

另请参阅 QFontMetricsF 和FontMetrics 。

属性文档

advanceWidth : real [read-only]

该属性存储了text 中字符的间距(以像素为单位)。这是从字符串当前位置到应绘制下一个字符串位置之间的距离。

另请参阅 QFontMetricsF::horizontalAdvance()。

boundingRect : rect [read-only]

该属性存储由text 指定的字符串中字符的边界矩形。

另请参阅 QFontMetricsF::boundingRect() 和tightBoundingRect 。

elide : enumeration

该属性用于指定文本的省略模式。它决定了字符串被省略的位置。可能的取值包括:

Constant描述
Qt::ElideNone不进行省略;这是默认值。
Qt::ElideLeft例如:“...World”
Qt::ElideMiddle例如:“He...ld”
Qt::ElideRight例如:“Hello...”

另请参阅 elideWidth 和QFontMetrics::elidedText 。

elideWidth : real

该属性指定文本在发生截断之前所能达到的最大宽度(以像素为单位)。

另请参阅 elide 和QFontMetrics::elidedText 。

elidedText : string [read-only]

如果字符串text 的宽度大于elideWidth ,则该属性将保存该字符串的省略版本(即包含“...”的字符串)。如果文本的宽度不超过elideWidth ,或者elide 被设置为Qt::ElideNone ,则该属性将等于原始字符串。

另请参阅 QFontMetricsF::elidedText()。

font : font

该属性存储用于度量计算的字体。

height : real [read-only]

该属性存储由text 指定的字符串中字符的边界矩形的高度。它等同于:

textMetrics.boundingRect.height

另请参阅 boundingRect 。

renderType : enumeration [since 6.3]

覆盖此组件的默认渲染类型。

支持的渲染类型包括:

常量描述
TextEdit.QtRendering文本通过针对每个字符的可缩放距离字段进行渲染。
TextEdit.NativeRendering文本使用特定于平台的技术进行渲染。

这应与您绘制文本时所设定的目标renderType 相匹配。

该属性在 Qt 6.3 中引入。

另请参阅 Text.renderType 。

text : string

该属性存储用于指标计算的文本。

tightBoundingRect : rect [read-only]

该属性会在由text 指定的字符串中的字符周围保持一个紧凑的边界矩形。

另请参阅 QFontMetricsF::tightBoundingRect() 和boundingRect 。

width : real [read-only]

该属性存储由text 指定的字符串中字符的边界矩形宽度。它等同于:

textMetrics.boundingRect.width

另请参阅 boundingRect 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.