Qml text wrap (max width)

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

    You can also try something like this, using the dummy text box mentioned above:

    width: Math.min(dummy_text.paintedWidth, 250)
    

    This will use the painted size of the text unless it is greater than your specified pixel width.

    0 讨论(0)
  • 2021-02-19 03:37

    i did not use state, but i use the idea of dummy text to have width. thanks

    my code :

                    Rectangle{
                    id:messageBoxCadre
                    width: (modelData.messageLength>25)? (wrapper.width - 20): messageBox.width+10
                    height: messageBox.height+5
                    color: modelData.myMessage ? "#aa84b2":"#380c47"
                    radius: 10
    
                    Text {
                        id:messageBox
                        width: (modelData.messageLength>25)? (wrapper.width - 20): dummy_text.dummy_text
                        text: '<b><font color=purple>'+modelData.message+'</font></b> '
                        wrapMode: "WordWrap"
                    }
    
                    Text {
                          id: dummy_text
                          text: '<b><font color=purple>'+modelData.message+'</font></b> '
                          visible: false
                      }
    
                }
    
    0 讨论(0)
  • 2021-02-19 03:38

    Try this:

    Text {
        property int MAX_WIDTH: 400
        width: MAX_WIDTH
        onTextChanged: width = Math.min(MAX_WIDTH, paintedWidth)
    }
    
    0 讨论(0)
  • 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
            }
        }     
    }
    
    0 讨论(0)
  • 2021-02-19 03:48

    Very late to the party but the clean solution is to use an embedded TextMetrics object. Like this:

    ...
    Text {
      id: textObj
      width: Math.min(textWidth, myThreshold)
    
      // access to binding-loop-free width and height:
      readonly property alias textWidth: textMetrics.boundingRect.width
      readonly property alias textHeight: textMetrics.boundingRect.height
    
      TextMetrics {
        id: textMetrics
        font: textObj.font
        text: textObj.text
        elide: textObj.elide
      }
    }
    
    0 讨论(0)
  • 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: '<b><font color=purple>'+modelData.message+'</font></b> '
                    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.

    0 讨论(0)
提交回复
热议问题