gpt4 book ai didi

qt - 使 QT/QML TextArea 滚动到底部

转载 作者:行者123 更新时间:2023-12-04 13:40:56 36 4
gpt4 key购买 nike

我在 QT/QML 5.11 上遇到了这么简单的事情,我几乎认为此时库中有一个错误。

我有以下代码:

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 计算是正确的)。

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

换句话说,在我的计算之后, contentY 在没有我干预的情况下神奇地回到了零,这当然打破了整个事情。

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

最佳答案

您可以使用 Flickable 或 ScrollView 来实现它。
将光标位置更改为在 TextArea 中将自动聚焦在该行上。
在文本区域创建一个函数并在每次文本更改时更改焦点。你必须手动调用它

 cursorPosition = length-1
或者
你可以直接设置属性
cursorPosition : length-1 
在每次文本更新的 TextArea 块中。
     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++))
}
}

关于qt - 使 QT/QML TextArea 滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223143/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com