简体   繁体   English

QML ListView 的可见项

[英]Visible items of QML ListView

I'm trying to get all visible items (ie only items visible by eyes) of QML ListView, but i'm not shure how to realize this functionality.我正在尝试获取 QML ListView 的所有可见项目(即仅眼睛可见的项目),但我不确定如何实现此功能。 Maybe is some working approach exists?也许存在某种工作方法? Thank you in advance.提前谢谢你。

indexAt( real x, real y ) can be used to calculate an index range of visible items. indexAt( real x, real y ) 可用于计算可见项的索引范围。

https://doc.qt.io/qt-5/qml-qtquick-listview.html#indexAt-method https://doc.qt.io/qt-5/qml-qtquick-listview.html#indexAt-method

I'm not aware of a function that'll give you an index range for visible items.我不知道有一个函数可以为您提供可见项目的索引范围。 You'll have to come up with your own implementation based on item sizes and your specific layout.您必须根据项目大小和特定布局提出自己的实施方案。

Here's one possible implementation:这是一种可能的实现:

ListView {
    id: listView

    function getVisibleIndexRange() {
        var center_x = listView.x + listView.width / 2
        return [indexAt( center_x, listView.y + listView.contentY + 10),
                indexAt( center_x, listView.y + listView.contentY + listView.height - 10)]
    }
}

This is the implementation I'm using:这是我正在使用的实现:

    id: listView

    onContentXChanged: {
        var idx = 0
        if ( listView.atXBeginning ) {
            idx = 0
        }
        else if ( listView.atXEnd ) {
            idx = listViewModel.count - 1
        }
        else {
            idx = listView.indexAt( listView.contentX + listView.width / 2,
                                    listView.y + listView.height / 2 )
        }

        //Do things with idx
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM