list 获取当前显示的委托索引 - QML ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25162940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:05:14  来源:igfitidea点击:

Get index of the delegate currently displayed - QML ListView

qtlistlistviewqml

提问by marmistrz

I created a ListView, which displays a couple of pages of content defined by the user (plain text). The page displayed is a delegate. Only one page is visible at a time. I decided to use it to get snapping to one item, in the same way the iOS' launcher works. The user simply flicks between the pages. (this is to be used on touch screens)

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

I need to have the index of the currently displayed page for some operation. currentIndex of the ListView always stays == 0. How can I get it?

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

For those who prefer code:

对于那些喜欢代码的人:

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

Thanks

谢谢

回答by mcchu

There are many ways to get the index of current item that is displayed in the screen. If you can get the x-y coordinate of current page, you can use indexAtmethod in ListView.

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

And in each delegate, you can find the index using indexrole within the scope of the delegate. The indexis like a role you declared in your model, and is automatically assigned by ListView. For example,

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

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

The indexrole is introduced here:

index角色介绍在这里

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...

包含模型中项目索引的特殊索引角色也可用于委托。请注意,如果该项目从模型中删除,则此索引设置为 -1...

Another way is to explicitly assign value to the currentItemproperty in ListView, so the view can scroll by itself. Here is an simple examplein Qt documentation, which is similar to your application.

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

回答by Neilana

I know this is quite old but I had the same problem and spend some time trying to find a way to get currentIndexthat would work for me. In my case I need to change the width of my ListViewso I had to recalculte currentIndexmanualy every time.

我知道这已经很老了,但我遇到了同样的问题,并花了一些时间试图找到一种方法来获取对我有用的 currentIndex。就我而言,我需要更改ListView的宽度,因此每次都必须手动重新计算currentIndex

But then I found a highlightRangeModeproperty. When it's set to ListView.StrictlyEnforceRangethen currentIndexis always updated automaticly and contains correct index of the currently visible item.

但后来我发现了一个highlightRangeMode属性。当它设置为ListView.StrictlyEnforceRange然后currentIndex总是自动更新并包含当前可见项目的正确索引。

ListView {
    highlightRangeMode: ListView.StrictlyEnforceRange 
    // ...
}

回答by Vasiliy

You can use attached properties of ListView class (ListView). They are attached to each instance of the delegate.

您可以使用 ListView 类 ( ListView) 的附加属性。它们附加到委托的每个实例。

See ListView.isCurrentItem or ListView.view example:

请参阅 ListView.isCurrentItem 或 ListView.view 示例:

    ListView {
        width: 180; height: 200

        Component {
            id: contactsDelegate
            Rectangle {
                id: wrapper
                width: 180
                height: contactInfo.height
                color: ListView.isCurrentItem ? "black" : "red"
                Text {
                    id: contactInfo
                    text: name + ": " + number
                    color: wrapper.ListView.isCurrentItem ? "red" : "black"
                }
            }
        }

        model: ContactModel {}
        delegate: contactsDelegate
        focus: true
    }

回答by Bhaskar Kumar Singh

You can do like that:

你可以这样做:

QModelIndex index =this->indexAt(event->pos());
        this ->setCurrentIndex(index);