1
votes

In the Documentation of Itemyou can find this:

Note: Using implicitWidth of Text or TextEdit and setting the width explicitly incurs a performance penalty as the text must be laid out twice.

So I should not write something like this:

Text {
    width: implicitWidth
    text: 'my text defines the width'
}

This means, if I don't set the width by some other means, I can not meaningful anchor to the Text.

I wonder if the same performance penalty applies to this structure:

Item {
    id: myAnchorableTextBoundingBox
    width: myText.implicitWidth
    height: myText.implicitHeight
    Text {
        id: myText
        text: 'my text defines the width'
    }
}

Or is this a probable workaround for this usecase?

It even allows me to elide a maximum width like this:

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
    }
}

I would imediately build a Component out of this, if I can be sure, there won't be (too large) a penalty.

1

1 Answers

0
votes

This workaround should work just fine. As the documentation states, the performance penalty is only an issue when setting the width as well:

Using implicitWidth [...] and setting the width explicitly incurs a performance penalty

In this case, you are only reading the implicitWidth property, and not then setting the width of the Text element, so the warning does not apply.