How to wrap some text in a rectangle in QML? -


i have perform simple task: want display piece of text inside rectangle , size of rectangle should precisely width of text.

in c++, it's easy do. define qstring , apply qfontmetrics width. define rectangle graphics element have size. it's done within 5 minutes.

i have heard qml easier use. therefore, expecting solve problem in less 5 minutes. didn't, , i'm still stuck @ it. here's have tried:

rectangle {     width: mytext.contentwidth     height: mytext.contentheight      text {         anchors.fill:parent         id: mytext         font.family: "helvetica"         font.pointsize: 50         text:  qstr("the string want display")     } } 

this doesn't work reason don't understand. have found way in way doesn't suits needs:

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 want display")     } } 

in case, when click rectangle, gets correct width. nevertheless, not interested in solution, because don't want have click rectangle correct size. want rectangle's size gets correct size whenever mytext changes text. use of ontextchanged in text item doesn't work either.

what missing here?

thanks ...

as far know, font metrics made available developers in qt 5.4, relatively new, in qml. got fontmetrics , textmetrics. simple usage example:

import qtquick 2.4 import qtquick.window 2.2  window {     visible: true     width: 280; height: 150      textmetrics {         id: textmetrics         font.family: "arial"         font.pixelsize: 50         text: "hello world"     }      rectangle {         width: textmetrics.width         height: textmetrics.height         color: "steelblue"          text {             text: textmetrics.text             font: textmetrics.font         }     } } 

as noted phrogz textmetrics type not support measuring wrapped text.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -