Qml text wrap (max width)

后端 未结 7 1827
抹茶落季
抹茶落季 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:52

    Obviously a couple years late, but I just ran into a similar issue (though I am using elide instead of wrap but the basics are the same). I ended up with what seems like a simple and clean solution so I figured if anyone else runs into this problem it can maybe help. Using the original code as an example:

            property int maxWidth: 100  // however you want to define the max width
    
            Rectangle{
                id:messageBoxCadre
                width: messageBox.paintedWidth+10  // width of the actual text, so your bubble will change to match the text width
                height: messageBox.height+5
                color: modelData.myMessage ? "#aa84b2":"#380c47"
                radius: 10
    
                Text {
                    id:messageBox
                    text: ''+modelData.message+' '
                    width: maxWidth  // max width that your text can reach before wrapping
                    wrapMode: "WordWrap"
                }
            }
    

    The only problem for this example is that with WordWrap, if a word is too long to fit the entire width of the Text item it will exceed whatever maxWidth you have set.

提交回复
热议问题