额外增加一层的Text, 并通过绑定父级的Text文本属性,即使父级的文本的width属性改变也不会影响到textLength的获取。
1Text {
2 text: "text"
3 font.pixelSize: 20
4
5 Text {
6 id: _text
7 visible: false
8 text: parent.text
9 font.pixelSize: parent.font.pixelSize
10 }
11
12 function textLength() {
13 return _text.width;
14 }
15
16 Component.onCompleted: console.log(textLength())
17}