英文:
Slint - Let Text widget self-layout its width and read the width in Rust?
问题
我需要在 Slint 中实现无限滚动文本。
简单的解决方法:将字符串放置在 Text
小部件中两次,并使用定期重复的计时器减少偏移量 offset_x
。如果偏移量小于文本宽度的一半(-width / 2),则添加回一半的宽度(+= width / 2)。
这里的问题是:一旦Text
的宽度绑定到一个我可以在代码中访问的属性,Slint 就停止布局Text
的宽度,其文本甚至可能被截断,无法知道它必须多宽才能容纳其文本。因此,我不知道何时重置滚动偏移量 offset_x
。
如何强制 Slint 布局 Text
的宽度并仍然让我在 Rust 代码中读取宽度?
Slint 代码:
in-out property <length> offset_x: -10px;
out property <length> out_width: 100px;
Text {
x: offset_x;
width: out_width;
}
英文:
I need to implement an endless Text-Scroll in Slint.
Easy solution: Put the string twice in a Text
widget and decrease the offset offset_x
by a regular repeating timer. If it goes less than half the text width (-width / 2) add back half the width (+= width / 2).
Here is the problem: As soon as the Text
width is bound to a property, which I can access in code, Slint stops layouting the Text
width, its Text may even be cut off, and it's impossible to know how wide it must be to contain its text. Therefore I don't know when to reset the scroll offset offset_x
.
How can I force Slint to layout the Text
width and still let me read the width in Rust code?
Slint-Code:
in-out property <length> offset_x: -10px;
out property <length> out_width: 100px;
Text {
x: offset_x;
width: out_width;
}
答案1
得分: 0
我找到了解决方案:
将宽度作为绑定属性引用。就像这样:
out property out_width <=> t.width;
t := Text {
x: offset_x;
}
英文:
I found the solution:
Reference the width as bound property. Like this:
out property out_width <=> t.width;
t := Text {
x: offset_x;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论