Search code examples
c++qtqmlqtquickcontrols2

QML Label max width & multiline


I have a Label, but it should only be as long as the form. If the label is longer it should start on a new line. How can I realize this? I am using QtQuick Controls 2.0

My current code for the Label:

Label {
    id: lblMsg
    width: ApplicationWindow.width - 10 // not working
    text: "ajksdlldjklasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd"
    x: 20
    y: 20
}

Solution

  • Use the wrapMode property:

    Label {
        id: lblMsg
        width: ApplicationWindow.width - 10
        text: "ajksdlldjklasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd"
        x: 20
        y: 20
        wrapMode: Label.WordWrap
    }
    

    I'd suggest using Qt Quick Layouts to manage the layout of your application instead of sizing items manually.