dynamic margin according to wrapped text
Solved
QML and Qt Quick
-
Hi ,
is there a way to dynamically change margins of a Label when the text is wrapped or not ?
I try this one but it doesn't work :Button { id: root width: 146 height: 60 background: Rectangle { id: buttonBackground anchors.fill: parent color: "#333" border.width: 0 radius: height / 2 } contentItem: Label { text: root.text font: root.font anchors.fill: parent anchors.leftMargin: contentWidth > width ? 12 : 4 anchors.rightMargin: contentWidth > width ? 12 : 4 horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter wrapMode: Label.Wrap elide: Text.ElideRight color: CustomColor.hightlightedText }
Any idea ?
Thanks -
How does that code work on your end?
It seems to work fine...
What do you want actually? -
Try using
lineCount > 1
if you want to check whether the text is wrapped. -
Yes great, thank you.
I add another thing which is to take into consideration the number of lines once the component is completed, because if I manage the margin directly during initialization, it does not work:anchors.leftMargin: contentWidth > width ? 12 : 4
anchors.rightMargin: contentWidth > width ? 12 : 4... Component.onCompleted: { anchors.leftMargin = (lineCount > 1 ? 12 : 4) anchors.rightMargin = (lineCount > 1 ? 12 : 4) }
-