隐藏ListView

时间:2015-09-16 11:22:33

标签: qt listview qml qtquick2

我已经学习QtQuick大约一个星期了,我正面临着一个奇怪的行为,我想要实现的目标。我想用键盘导航制作垂直ListView,这样当我按下向上或向下时,项目会向上或向下移动,如果某个项目进入或退出"视口",其opacity媒体资源将平稳更改为01

这是我目前的QML代码:

import QtQuick 2.4

Rectangle {
    width:200
    height:400
    ListView {
        property int activePosition:1
        property int itemDisplayed:3
        width:parent.width-50
        height:parent.height-50
        anchors.centerIn:parent
        model:10
        snapMode:ListView.SnapToItem
        focus:true
        cacheBuffer:2000
        Component.onCompleted: {
            console.log(count+' != '+contentItem.children.length+' ???')
        }
        Keys.onPressed: {
            var i = 0
            console.log('pos='+activePosition)
            console.log(count+' != '+contentItem.children.length+' ???')
            if (event.key === Qt.Key_Up) {
                if (activePosition == 1 && currentIndex > 0) {
                    i = currentIndex+itemDisplayed-1
                    if (i < contentItem.children.length - 2/* why -2 instead of -1 ??? */) {
                        console.log('out='+i)
                        contentItem.children[i].state = 'out'
                    }
                }
                activePosition = activePosition > 1 ? activePosition - 1 : activePosition
            }
            if (event.key === Qt.Key_Down) {
                if (activePosition == itemDisplayed && currentIndex < contentItem.children.length - 2) {
                    i = currentIndex-itemDisplayed+1
                    if (i >= 0) {
                        console.log('out='+i)
                        contentItem.children[i].state = 'out'
                    }
                }
                activePosition = activePosition < itemDisplayed ? activePosition + 1 : activePosition
            }
        }

        delegate: Rectangle {
            id:rect
            state:index < ListView.view.itemDisplayed ? 'in' : 'out'
            opacity:1.0
            width:ListView.view.width
            height:ListView.view.height/ListView.view.itemDisplayed
            border.color:'white'
            border.width:1
            color:activeFocus ? 'red': 'gray'
            onActiveFocusChanged: {
                if (activeFocus) {
                    state = 'in'
                    console.log('in='+index)
                }
            }
            states: [
                State { name:'in'; PropertyChanges { target:rect; opacity:1.0 } },
                State { name:'out'; PropertyChanges { target:rect; opacity:0.0 } }
            ]
            transitions: [
                Transition {
                    to:'in'
                    NumberAnimation { property:'opacity'; duration:250 }
                },
                Transition {
                    to:'out'
                    NumberAnimation { property:'opacity'; duration:250 }
                }
            ]
            Text {
                text:index
                anchors.centerIn:parent
            }
        }
    }
}

第一个问题:模型= 10,为什么model.count不等于contentItem.children.lengthonCompleted给出5对11和导航10对11

第二个问题:如果我按UP或DOWN,它可以正常工作,直到我达到指数= 4,为什么?

因为我是QtQuick的初学者,所以也许这不是正确的方法。我尝试使用visible属性,但每个项目都有visible = true,即使它们在外面。我也尝试indexAt()但没有成功。

任何帮助都会很棒: - )

1 个答案:

答案 0 :(得分:0)

现在我对ListView行为了解得更清楚了。我之前的代码可以通过删除不再有用的Keys.onPressed事件并将itemAt()方法直接用于onActiveFocusChanged处理程序来修复。