Qml text wrap (max width)

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

    Here's another way, which uses the Component.onCompleted script. It's more static than my other method, so I guess it depends on what you want to do with it.

    import QtQuick 1.0
    
    Rectangle {
        id: containing_rect
        property string text
    
        height: text_field.paintedHeight
    
        text: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"
        //text: "a short string"
    
        Text {
            id: text_field
            anchors.top: parent.top
            anchors.left: parent.left
    
            height: parent.height
            width: parent.width
    
            text: parent.text
            wrapMode: Text.WordWrap
        }
    
        Component.onCompleted: {
            if (text_field.paintedWidth > 200) {
                width = 200
            } else {
                width = text_field.paintedWidth
            }
        }     
    }
    

提交回复
热议问题