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

标签 qt list listview qml

我创建了一个 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/

相关文章:

python - 将整数转换为其 ascii 值的字符串

c# - 在单独的行中列出项目

c++ - QT : cannot call member without object when I have a virtual function

python - 在 python 中进行 Squish 测试以测试 qt 应用程序

python - 比较两个列表以获得不匹配的元素

python - 重复 for 循环直到完成?

c# - 在 ListView 的下拉列表中获取 selectedIndexChanged 上的 ListView 项的值

android - 捕获图像后如何更改 ListView 行图标?

qt - QML 显示动画

c++ - 实例声明后 ';' 之前缺少 '*'