Qml text wrap (max width)

后端 未结 7 1790
抹茶落季
抹茶落季 2021-02-19 02:56

I would like to put text inside a bubble, and I want that my bubble be equal to the text width, but if the text length is too long, I would like the text to wrap automatically a

7条回答
  •  别那么骄傲
    2021-02-19 03:48

    Very late to the party but the clean solution is to use an embedded TextMetrics object. Like this:

    ...
    Text {
      id: textObj
      width: Math.min(textWidth, myThreshold)
    
      // access to binding-loop-free width and height:
      readonly property alias textWidth: textMetrics.boundingRect.width
      readonly property alias textHeight: textMetrics.boundingRect.height
    
      TextMetrics {
        id: textMetrics
        font: textObj.font
        text: textObj.text
        elide: textObj.elide
      }
    }
    

提交回复
热议问题