我有一个QML TextItem,其中文本已动态设置。宽度我无法确定,因为文本可以有任意数量的字符。我需要在它旁边放置另一个文本项。所以当我们无法确定文本的宽度时,如何锚定到文本项的右侧,因为文本可以随时增长。不允许包裹或长者
例如:
Text
{
id: distance
width: //dont know what to provide
font.bold: true
verticalAlignment:Text.AlignBottom
horizontalAlignment:Text.AlignLeft
maximumLineCount: 1
onTextChanged:
{
console.log("$$$$$___Text is been changed:" + txt_distance.paintedWidth +" "+ paintedHeight)
}
anchors
{
bottom: parent.bottom
bottomMargin: 2
left: parent.left
leftMargin: 20
right: //Dont know what to give
}
和邻居项目
Text
{
id: address
font.bold: true
verticalAlignment:Text.AlignBottom
horizontalAlignment:Text.AlignLeft
maximumLineCount: 1
anchors
{
bottom: parent.bottom
bottomMargin: 2
left: distance.right <-- Please help
right: parent.right
rightMargin: 20
}
}
您不必显式设置width
,因为它将设置为paintedWidth
。
您的示例可以简化为:
Text {
id: distance
text: "Hello"
}
Text {
text: "Wagmare"
anchors.left: distance.right
}
更好的解决方案是使用Row
:
Row {
Text {
text: "Hello"
}
Text {
text: "Wagmare"
}
}
见Use Case - Displaying Text In QML
如果未明确设置宽度或高度,则读取这些属性将返回文本边界矩形的参数(如果已明确设置宽度或高度,则仍可使用paintedWidth和paintedHeight)。