[英]QML Flickable with TextArea: property binding for contentY is overwritten - by whom?
I'm making a terminal widget.我正在制作一个终端小部件。 I want the
Flickable
to scroll down to the latest input when TextArea.text
is updated.我希望
Flickable
在TextArea.text
更新时向下滚动到最新输入。 My code looks like as follows.我的代码如下所示。
ColumnLayout {
anchors.fill: parent
Flickable {
id: scroller
clip: true
contentY: contentHeight - height
onContentYChanged: {
console.log("contentY:", contentY)
}
TextArea.flickable: TextArea {
id: textArea
Layout.fillWidth: true
}
Layout.fillWidth: true
Layout.fillHeight: true
}
RowLayout {
id: prompt
Label {
text: " > $ "
}
TextField {
id: textInput
Layout.fillWidth: true
}
}
}
When I run this, I see that contentY
is being overwritten right after it's set by my binding:当我运行它时,我看到
contentY
在我的绑定设置后立即被覆盖:
qml: contentY: 1498
qml: contentY: 0
qml: contentY: 1517
qml: contentY: 0
I checked to make sure my binding is not setting it to 0. I tried to debug the binding loop using export QT_LOGGING_RULES="qt.qml.binding.removal.info=true"
, that came out clean.我检查以确保我的绑定没有将其设置为 0。我尝试使用
export QT_LOGGING_RULES="qt.qml.binding.removal.info=true"
调试绑定循环,结果很干净。 I had a look at the source for Flickable
and I don't think any of the methods there are the culprit.我查看了
Flickable
的来源,我认为没有任何方法是罪魁祸首。
Is binding contentY
the right way to do what I want?绑定
contentY
是做我想做的事情的正确方法吗? Why is my binding is not being respected?为什么我的绑定不被尊重?
The issue is that contentY
is going to be overwritten constantly by the Flickable as the content inside of it moves around.问题是
contentY
将不断被 Flickable 覆盖,因为它里面的内容会四处移动。 You can't place a binding on it because as you can see, it will be immediately overwritten with a static value that updates as the user interacts with the flickable.您不能在其上放置绑定,因为如您所见,它将立即被 static 值覆盖,该值会随着用户与轻弹交互而更新。
What you need to do instead is something like你需要做的是类似
onContentHeightChanged: Qt.callLater(() => contentY = contentHeight - height)
Now, whenever the text area grows, it will jump the contentY via an immediate assignment instead of trying to rely on a binding.现在,每当文本区域增长时,它将通过立即分配而不是尝试依赖绑定来跳转 contentY。 The callLater ensures that it happens after the flickable resets contentY to 0 on its own due to the height change.
callLater 确保它发生在 flickable 由于高度变化而自行将 contentY 重置为 0 之后发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.