Qml text wrap (max width)

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

    You can almost do this neatly with states. The problem is that attempting to set the width of the parent by assigning it to the paintedWidth of the text box means it then sets the width of the text box, which QML detects as influencing paintedWidth. It wouldn't recurse further than this, but QML still kicks out warnings. One way around the problem is to do as follows, and have a dummy invisible text box that just works out how wide the text is/should be. Its a bit of a hack, but it works nicely.

    You could change the "when" property of the state to be dependent on the size of the dummy text box (rather than the length of the string) if you preferred a pixel limit on the width of the box.

    import QtQuick 1.0
    
    Rectangle {
        id: containing_rect
        property string text
    
        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
    
        }
    
        Text {
            id: dummy_text
            text: parent.text
            visible: false
        }
    
        states: [
                State {
                    name: "wide text"
                    when: containing_rect.text.length > 20
                    PropertyChanges {
                        target: containing_rect
                        width: 200
                        height: text_field.paintedHeight
                    }
                },
                State {
                    name: "not wide text"
                    when: containing_rect.text.length <= 20
                    PropertyChanges {
                        target: containing_rect
                        width: dummy_text.paintedWidth
                        height: text_field.paintedHeight
                    }
                }
            ]
    }
    
    0 讨论(0)
提交回复
热议问题