Search code examples
qmlqt5qtquick2qtquickcontrols

TextField can't be resized within RowLayout


After putting TextField in RowLayout I can't resize the TextField anymore. I tried to set anchors for TextField to fill left side of RowLayout and its center to make it the half of width of the RowLayout but it becames just bigger than the half of it.

Right now I'm trying to bind the width of the TextField to that of RowLayout but still the element just doesn't resize. When I take TextField out of its parent it resizes fine. Is this a bug of Qt or did I forget something?

Here is the pic of what I get:

enter image description here

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.2

ApplicationWindow {
    visible: true
    width: 210
    height: 160

    RowLayout {
        x: 15
        y: 21
        width: 181
        height: 23

        TextField {
            id: first
            width: parent.width /2
            height: parent.height
        }

        TextField {
            id: second
            width: parent.width /2
            height: parent.height
        }
    }

    TextField {
        id: result
        x: 15
        y: 55
        width: 181
        height: 23
        placeholderText: qsTr("Result")
    }
}

Solution

  • As @KernelPanic said, I used Layout.fillWidth on TextFields and everything began to work fine.

    According to Qt documentation:

    If this property is true, the item will be as wide as possible while respecting the given constraints. If the property is false, the item will have a fixed width set to the preferred width. The default is false, except for layouts themselves, which default to true.

    And

    respecting the given constraints

    was exactly what I needed