title: How to wrap some text in a rectangle in QML?
body: <p>I have to perform a very simple task: I want to display a piece of text inside a rectangle and the size of that rectangle should precisely be the width of the text. </p>
  
  <p>In C++, it's fairly easy to do. Just define the QString and apply the QFontMetrics to get its width. Then define the rectangle graphics element to have that size. It's done within five minutes. </p>
  
  <p>I have heard that QML is easier to use. Therefore, I was expecting to solve that problem in less than five minutes. I didn't, and I'm still stuck at it. Here's what I have tried:</p>
  
  <pre><code>Rectangle {
      width: myText.contentWidth
      height: myText.contentHeight
  
      Text {
          anchors.fill:parent
          id: myText
          font.family: "Helvetica"
          font.pointSize: 50
          text:  qsTr("The string I want to display")
      }
  }
  </code></pre>
  
  <p>This doesn't work for some reason I don't understand. I have found a way to do it in a way that doesn't exactly suits my needs: </p>
  
  <pre><code>Rectangle {
      width: 100
      height: 100
  
      MouseArea {
         id: myMouseArea
         anchors.fill: parent
         onClicked: parent.width=myText.contentWidth 
         hoverEnabled: true
       }
  
      Text {
          anchors.fill:parent
          id: myText
          font.family: "Helvetica"
          font.pointSize: 50
          text:  qsTr("The string I want to display")
      }
  }
  </code></pre>
  
  <p>In this case, when I click the rectangle, it gets the correct width. Nevertheless, I am not interested in this solution, because I don't want to have to click to get a rectangle with the correct size.</p>
  
  <p>I want that the rectangle's size gets the correct size whenever <code>myText</code> changes text. The use of <code>onTextChanged</code> in the Text item doesn't work either. </p>
  
  <p>What am I missing here?</p>