Vladimir Prus
Vladimir Prus

Reputation: 4640

QML Layout not limiting width of children

I have a QML ColumnLayout with a few children, and I want to limit its width, like so:

ColumnLayout {

    width: 360
    height: 480

    ColumnLayout {
        id: inner
        Layout.maximumWidth: 200
        Layout.fillHeight: true

        Text {
            text: "Welcome"
            font.pixelSize: 18
        }

        Text {
            text: "Long explanation that should wrap, but ends up overflowing layout"
            wrapMode: Text.WrapAnywhere
        }
    }

    Rectangle {
        anchors.fill: inner
        z: -1
        border.color: "orange"
    }

}

I get the result shown in the picture below.

enter image description here

The orange box shows the dimensions of the inner layout item, which is all right. However, the children text of the layout is not wrapped.

I can only fix that by adding

Layout.maximumWidth: parent.width

to my 'Text' items, but that's quite awkward approach. Am I doing anything wrong, or is this a Qt bug, or is it a design approach for some reason? I am using Qt 5.4.1.

Upvotes: 4

Views: 2020

Answers (1)

Nadarian
Nadarian

Reputation: 1002

From Text - wrapMode:

The text will only wrap if an explicit width has been set

Based on that:

Text
{
    width: parent.width
}

Upvotes: 2

Related Questions