gpt4 book ai didi

qt - 获取当前显示的委托(delegate)的索引 - QML ListView

转载 作者:行者123 更新时间:2023-12-03 07:17:25 28 4
gpt4 key购买 nike

我创建了一个 ListView,它显示用户定义的几页内容(纯文本)。显示的页面是委托(delegate)。一次只能看到一页。我决定使用它来捕捉一个项目,就像 iOS 启动器的工作方式一样。用户只需在页面之间滑动即可。 (这将在触摸屏上使用)

我需要当前显示页面的索引来进行某些操作。 ListView的currentIndex始终保持== 0。我怎样才能得到它?

对于那些喜欢代码的人:

 ListView
{
onCurrentIndexChanged: console.log(currentIndex) // this gets called only once - at startup
delegate: Column
{
// The page displayed, only one page at a time
}
}

谢谢

最佳答案

有很多方法可以获取屏幕中显示的当前项目的索引。如果可以获取当前页面的x-y坐标,可以使用indexAt ListView 中的方法。

在每个委托(delegate)中,您可以在委托(delegate)范围内使用 index 角色查找索引。 index 就像您在模型中声明的角色,由 ListView 自动分配。例如,

ListView 
{
delegate: Column
{
property int indexOfThisDelegate: index
//...
}
}

引入了index角色here :

A special index role containing the index of the item in the model is also available to the delegate. Note this index is set to -1 if the item is removed from the model...

另一种方法是显式地为 ListView 中的 currentItem 属性赋值,以便 View 可以自行滚动。这是一个简单的 example在 Qt 文档中,这与您的应用程序类似。

关于qt - 获取当前显示的委托(delegate)的索引 - QML ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25162940/

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