2

我的 QML 有问题。我想TextInput根据操作编辑 a ,将focus属性设置为true. 它在TextInput位于 a 中时有效Rectangle,但不在 a 中ScrollView。这是一个例子:

Item {
    id: main
    width: 640
    height: 480

    ScrollView{
        id: scrollView
        height: parent.height/2
        width: parent.width

        Rectangle{
            border.color: "black"
            border.width: 1
            anchors.centerIn: parent
            height: 25
            width: 200
            TextInput{
                id: ti1
                anchors.fill: parent
                verticalAlignment: TextInput.AlignVCenter
                horizontalAlignment: TextInput.AlignHCenter
            }
        }

    }

    Rectangle{
        y: height
        height: parent.height/2
        width: parent.width

        Rectangle{
            border.color: "black"
            border.width: 1
            anchors.centerIn: parent
            height: 25
            width: 200
            TextInput{
                id: ti2
                anchors.fill: parent
                verticalAlignment: TextInput.AlignVCenter
                horizontalAlignment: TextInput.AlignHCenter
            }
        }
    }

    MouseArea{
        anchors.fill: parent
        onClicked: {
            if (mouseY < parent.height/2){
                ti2.focus = false
                ti1.focus = true
            }else{
                ti1.focus = false
                ti2.focus = true
            }
        }
    }

}

当我单击窗口的下半部分时,TextInputti2 是可编辑的。但是当我点击上半部分时,ti1 不是。

有人知道吗?行为与 相同TextEdit

谢谢。

4

1 回答 1

3

我认为这是因为: “只有一个 Item 可以是 ScrollView 的直接子项,并且子项被隐式锚定以填充滚动视图。” .

来自:http ://doc.qt.io/qt-5/qml-qtquick-controls-scrollview.html

也许组件树在 ScrollView 中不可用。

但是如果你使用:

ti1.forceActiveFocus();

代替:

ti1.focus = true

有用。

于 2015-05-29T09:16:37.757 回答