在 QML 中使用 Text.implicitWidth 时的性能损失
Performance Penalty when using Text.implicitWidth in QML
在 Item
的文档中,您可以找到:
Note: Using implicitWidth of Text or TextEdit and setting the width explicitly incurs a performance penalty as the text must be laid out twice.
所以我不应该这样写:
Text {
width: implicitWidth
text: 'my text defines the width'
}
这意味着,如果我不通过其他方式设置宽度,我将无法锚定到 Text
。
我想知道同样的性能损失是否适用于此结构:
Item {
id: myAnchorableTextBoundingBox
width: myText.implicitWidth
height: myText.implicitHeight
Text {
id: myText
text: 'my text defines the width'
}
}
或者这是否是此用例的可能解决方法?
它甚至允许我像这样省略最大宽度:
Rectangle {
color: 'transparent'
border.color: 'red'
width: myText.width + 2
height: myText.height + 2
}
Rectangle {
id: myAnchorableTextBoundingBox
y: 1
x: 1
border.color: 'black'
width: myText.truncated ? myText.width : myText.implicitWidth
height: myText.implicitHeight
Text {
id: myText
text: 'my text defines the width until it elides, then the width is used as limit'
elide: Text.ElideRight
width: 200
}
}
我会立即从中构建一个 Component
,如果我可以确定,不会有(太大的)惩罚。
此解决方法应该可以正常工作。正如文档所述,性能损失只是设置宽度时的一个问题:
Using implicitWidth [...] and setting the width explicitly incurs a performance penalty
在这种情况下,您只是读取 implicitWidth
属性,而不是设置 Text
元素的 width
,因此警告不适用.
在 Item
的文档中,您可以找到:
Note: Using implicitWidth of Text or TextEdit and setting the width explicitly incurs a performance penalty as the text must be laid out twice.
所以我不应该这样写:
Text {
width: implicitWidth
text: 'my text defines the width'
}
这意味着,如果我不通过其他方式设置宽度,我将无法锚定到 Text
。
我想知道同样的性能损失是否适用于此结构:
Item {
id: myAnchorableTextBoundingBox
width: myText.implicitWidth
height: myText.implicitHeight
Text {
id: myText
text: 'my text defines the width'
}
}
或者这是否是此用例的可能解决方法?
它甚至允许我像这样省略最大宽度:
Rectangle {
color: 'transparent'
border.color: 'red'
width: myText.width + 2
height: myText.height + 2
}
Rectangle {
id: myAnchorableTextBoundingBox
y: 1
x: 1
border.color: 'black'
width: myText.truncated ? myText.width : myText.implicitWidth
height: myText.implicitHeight
Text {
id: myText
text: 'my text defines the width until it elides, then the width is used as limit'
elide: Text.ElideRight
width: 200
}
}
我会立即从中构建一个 Component
,如果我可以确定,不会有(太大的)惩罚。
此解决方法应该可以正常工作。正如文档所述,性能损失只是设置宽度时的一个问题:
Using implicitWidth [...] and setting the width explicitly incurs a performance penalty
在这种情况下,您只是读取 implicitWidth
属性,而不是设置 Text
元素的 width
,因此警告不适用.