gpt4 book ai didi

javascript - 记住 QML 元素中的滚动位置

转载 作者:数据小太阳 更新时间:2023-10-29 05:23:41 26 4
gpt4 key购买 nike

我有一些 qml 作为应用程序的输出(有点像只读控制台)。但是,使用起来很烦人,因为它在打印信息时会滚动回顶部。

例如,假设我每秒向我的 TextArea 打印一行,大约一分钟后,我将有足够的行,现在我有一个滚动条。我滚动到底部,一秒钟后,打印了一行文本,导致它滚动回顶部。

我想要的理想功能是自动滚动到底部(很像控制台打印内容时的样子),除非用户通过向上滚动来覆盖它,那么文本应该保持不变。我希望这是有道理的,这里是一些代码:

        ScrollArea {
id: helpTextScrollArea
anchors.left: parent.left
anchors.right: parent.right
anchors.top: myButton.bottom
anchors.topMargin: 5
anchors.bottom: parent.bottom
visible: false
horizontalScrollBar.visible: false

onVisibleChanged: {
helpText.visible = visible
}

HelpTextArea {
id: helpText
width: parent.parent.width - helpTextScrollArea.verticalScrollBar.width
text: "Oops, no documentation."

onVisibleChanged: {
if(helpTextScrollArea.visible != visible) {
helpTextScrollArea.visible = visible
}
}
}
}

Flickable
{
id: flick

anchors.left: parent.left
anchors.right: parent.right
anchors.top: runStopButton.bottom
anchors.bottom: parent.bottom
anchors.topMargin: 5


TextArea{

id: messageArea
anchors.fill: parent

focus: true

readOnly: true

visible: !helpTextScrollArea.visible

wrapMode: TextEdit.Wrap

function addText(newText) {
text += newText + "\n"
}
}
}

注意:我认为我的 Flickable 没有任何作用,这是我解决问题的实验的一部分。另外,我使用函数 addText 打印到文本区域。我对 qml 几乎一无所知,大部分代码都是别人写的,我正在尝试使用它。谢谢!

最佳答案

您可以将 FlickablecontentY 属性绑定(bind)到将计算正确位置的表达式。

Flickable {
id: flick
states: State {
name: "autoscroll"
PropertyChanges {
target: flick
contentY: messageArea.height - height
}
}
onMovementEnded: {
if (contentY === messageArea.height - height) {
state = "autoscroll"
}
else {
state = "" // default state
}
}
// ...
}

关于javascript - 记住 QML 元素中的滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914692/

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