[Interest] QML Text.contentWidth is never increasing

Kristoffersen, Even (NO14) Even.Kristoffersen at Honeywell.com
Fri Oct 21 15:54:20 CEST 2016


> > Jason H wrote:
> > >Text {
> > >	id: instructionText
> > >	color: "white"
> > >	text: instructionTextData
> > >	width: contentWidth > parent.width *.9 ? parent.width *.9 : contentWidth
> > >	font.pointSize: 16
> > >	horizontalAlignment: Text.AlignHCenter
> > >	anchors.horizontalCenter: parent.horizontalCenter }
> >
> >Where I want the text to be it's natural width up to 90% of the parent width, then wrap, but it only works when initially laying out and shrinking. 
> 
> > I think I'm missing some use case, why bother setting the width to contentWidth?
> > You anyway center the Text element and the text inside it so nothing prevents you from always setting it to parent.width *.9?
> > The text will then still be centered inside the Text element, and the Text element centered in its parent.

> Good idea. Visually it is the same, but not when you have a MouseArea that you only want to apply to the text.
> [------------text-------------] (your solution, setting text align) vs
> ------------[text]------------- (my desired situtation, because there is another MouseArea behind it) (Stuff in brackets is child MouseArea with anchors.fill:parent)

> Also I'm not changing contentWidth. According to my interpretation of the docs, contentWidth should not change with a wrapMode set.

And there's the use case I was missing :-)

I read the doc for contentWidth a bit differently.
I believe it only gives width greater than Text.width if the wrapping is unable to wrap the text completely.
IE. If you have wrapMode: Text.WordWrap enabled and have a complete word that is longer than width, contentWidth will be set to that width.
If the wrap succeeds it will give the width of the wrapped content.

This is a bit clearer if you see the doc for wrapMode:
[quote]Text.WordWrap - wrapping is done on word boundaries only. If a word is too long, contentWidth will exceed a set width.[/quote]


I guess one workaround would be to center the MouseArea in the Text element and set the width of it to Text.contentWidth. (Have not tested it tho)

-Even


More information about the Interest mailing list