使QT/QML文本区域滚动到底部

使QT/QML文本区域滚动到底部,qt,qml,qt5,Qt,Qml,Qt5,我在使用QT/QML 5.11时遇到了困难,因为这么简单的事情,我几乎认为在这一点上库中有一个bug 我有以下代码: Flickable { id: txflick anchors.top: title_label.bottom anchors.bottom: crect.bottom anchors.right: crect.right anchors.left: busy_ind.right flickableDirection: Flick

我在使用QT/QML 5.11时遇到了困难,因为这么简单的事情,我几乎认为在这一点上库中有一个bug

我有以下代码:

Flickable {
    id: txflick
    anchors.top: title_label.bottom
    anchors.bottom: crect.bottom
    anchors.right: crect.right
    anchors.left: busy_ind.right

    flickableDirection: Flickable.VerticalFlick

    onContentYChanged: console.log("contentY_changed", this.contentY)
    //contentY: txarea.contentHeight - txarea.height
    interactive: false // Has no effect, contentY keeps changing to zero

    TextArea.flickable: TextArea {
        id: txarea

        topPadding: 8
        bottomPadding: 10
        leftPadding: 10
        rightPadding: 10
        readOnly: true
        text: menu_view.pwrcon.text

        onTextChanged: {
            console.log("text changed")
            txflick.contentY = txarea.contentHeight - txflick.height
            console.log("chg", txarea.contentHeight - txflick.height)
            console.log(text)
        }

        onContentHeightChanged: {
            console.log("ctheight = ___", contentHeight, height, txflick.height, txflick.contentHeight)
        }

        font.family: "DejaVu Sans Mono,Ubuntu Sans Mono,Noto Sans Mono"
        font.bold: false
        font.pixelSize:12
        color: "black"
        //verticalAlignment: TextInput.AlignTop
        background: Rectangle { color: "lightgrey"; radius: 2;
            border.width: 1; border.color: "darkgrey" }
    }
}
基本上,TextArea的文本链接到“menu_view.pwrcon.text”,这在Python代码中被更改(这是一个属性)。当文本更改时,我希望它将flickable设置为文本的底部,以便我们看到最近添加的行

我也是

txflick.contentY = txarea.contentHeight - txflick.height
触发onTextChanged()事件时。没有问题,我检查了这些数字,结果很好(手动滚动到console.log()显示的数字显示contentY计算是正确的)

但是,似乎在我更改contentY之后,组件(flickable)单独将其更改回0(只有在文本高度大于flickable的固定高度之后,才会发生这种行为)。这真是太愚蠢了,我怀疑这是一个bug还是故意的

换言之,在我计算之后,满足感在没有我干预的情况下神奇地回到零,这当然破坏了整个过程


有什么方法可以解决这个问题吗?

您可以使用Flickable或ScrollView来实现

将光标位置更改为in-TextArea将自动聚焦于该行

在文本区域中创建一个函数,并在每次文本更改时更改焦点。你必须手动调用它

 cursorPosition = length-1
或 您可以直接设置属性

cursorPosition : length-1 
每次文本更新时都会在文本区域块中显示

     ScrollView {
            id: scrollView
            height: parent.height
            width: parent.width
            clip: true
            anchors.fill: parent

            TextArea {
                id: statusTextArea
                Layout.preferredWidth:parent.width
                Layout.fillHeight:  true
                readOnly:           true
                textFormat:         TextEdit.RichText
                text:              "Long Text \n"
                width: parent.width
                height: parent.height
                anchors.fill: parent
                function append(strAdd)
                {
                    statusTextArea.text = statusTextArea.text + strAdd
                    statusTextArea.cursorPosition = statusTextArea.length-1
                }
            }
        }

       Timer {
            running: true
            interval: 1000
            repeat: true
            property int iteration: 0

            onTriggered:{
                statusTextArea.append("Line %1\n".arg(iteration++))
                statusTextArea.append("Line %1\n".arg(iteration++))
             }
        }
您使用的文本区域是:或?如果这是第一次,那么我认为使用Flickable是没有必要的