qt - 如何以编程方式将文本附加到 QML TextArea?
问题描述
我正在尝试将日志数据一次传递到我的 QML 前端,并将其附加到 TextArea 的末尾。我考虑了几种方法。以下是最有希望的。我已经创建了一个 QAbstractListModel(在 Python 中)并将这个模型传递到一个转发器,它作为单个项目(rowCount = 1)到达,我使用该行附加到 TextArea
text: terminal_text.text + display
这可行,但每次更新文本时都会收到此警告。
file://.../TextArea.qml:728:9: QML QQuickTextEdit*: Binding loop detected for property "text"
中继器的代码见下文。
Repeater {
model: TerminalFeed { }
delegate: TextArea {
id: terminal_text
font.family: "Courier"
width: parent.width
height: parent.height
readOnly: true
selectByMouse: true
wrapMode: TextEdit.NoWrap
horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
verticalScrollBarPolicy: Qt.ScrollBarAsNeeded
text: terminal_text.text + display
}
}
我怎样才能阻止这种情况发生?或者有没有人有更好的方法来达到同样的结果?
解决方案
从技术上讲,这确实是一个绑定循环,因为text
它依赖于它自己的值。如果 QML 没有检测到它并破坏它,就会导致无限循环的更新。
您可以执行以下操作,而不是使用绑定:
Repeater {
model: TerminalFeed { }
delegate: TextArea {
id: terminal_text
font.family: "Courier"
width: parent.width
height: parent.height
readOnly: true
selectByMouse: true
wrapMode: TextEdit.NoWrap
horizontalScrollBarPolicy: Qt.ScrollBarAsNeeded
verticalScrollBarPolicy: Qt.ScrollBarAsNeeded
onDisplayChanged: {
text = text + display;
}
}
}
使用原始的绑定方法,它会在任何时候尝试更新display
或 text
更改。使用这种方法,它只会在发生变化时尝试更新display
——这正是你真正想要的。
推荐阅读
- amazon-web-services - Amazon Athena 错误打开 Hive 拆分 s3 路径并拒绝访问
- python - 在 Python Pandas 的创建函数中使用 .format() 从数据框中选择值时出错?
- python - 在 Telegram Python 中回复消息后如何停止代码?
- prometheus - 是否有任何方法可以获取用户的指标,例如 mem 和 cpu 使用情况?
- javascript - 如何在 Yup 结果中跳过空字符串?
- protocol-buffers - protobuf java 生成代码中的自定义
- c# - 通过 USB 的以太网 (USB NCM) 与 HoloLens2 的连接丢失!USB NCM 带宽有限?
- spring - Gradle projet,在heroku上部署期间出现错误NoSuchMethodException
- 3d - 为什么它在场景视图和游戏视图中没有变化?只有 Gizmo 改变
- reactjs - 如何在 React with Redux 中配置受控输入以进行状态管理?