Qt Quick Controls 2 TabBar/TabButton autosize to button text width
-
-
Setting a width works but then I need to manually calculate the width for every button in the TabBar - Is there a way to access the size of the text in the button?
Is it not what you get with implicit width? The implicit width of the button equals to the implicit width of the content item + left padding + right padding. For the built-in styles, the content item is a Text element.
PS. If you still need to calculate the width of a specific text string, QML TextMetrics (http://doc.qt.io/qt-5/qml-qtquick-textmetrics.html) can do that.
-
if I set width: implicitWidth the text does not show - Looking at the source it should be as you pointed out:
implicitWidth: Math.max(background ? background.implicitWidth : 0, contentItem.contentWidth + leftPadding + rightPadding)
But it seems the value is 0
-
I can't test right now, but I suspect it could be because it's using
contentWidth
instead ofimplicitWidth
. Feel free to report a bug. This is something we should ensure that it works and even make it easily configurable.Does it resize to contents if you do:
TabButton { width: contentItem.implicitWidth + leftPadding + rightPadding }
?