QtQuick2中Listview组件上的MouseArea事件

时间:2015-04-10 09:53:55

标签: qml qtquick2

我们正在开发触摸屏设备,我们遇到以下代码的问题:

import QtQuick 2.1
import QtQuick.Window 2.0

Window {
    visible: true
    id: box
    width: 360
    height: 360

    ListView {
        id: list
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        spacing: 0
        model: 30
        delegate: data
        clip: true
        focus: true
        smooth: true
    }
    Component{
        id: data
        Item{
            id: item
            width: list.width
            height: 30
            clip: true; smooth: true; visible: true

            property string cellColor: getCellColor(index)

            Rectangle{
                id: condData_item_line
                width: parent.width
                height: parent.height
                color: cellColor
                clip: true; smooth: true
                Text{
                    text: index
                    anchors.centerIn: parent
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        console.log("click", index, mouse.x, mouse.y)
                    }
                    onPressed : {
                        console.log("press", index, mouse.x, mouse.y)
                    }
                    onReleased: {
                        console.log("release", index, mouse.x, mouse.y)
                    }
                    onCanceled: {
                        console.log("cancel")
                    }
                }
            }
        }
    }

    function getCellColor(index){
        var color;
        if(index % 2 == 0){
            color = "white";
        }else{
            color = "blue";
        }
        return color;
    }
}

我们定义了clickedreleased事件的句柄,但从未调用它们。这是正常的吗?如果是,我们应该如何对事件处理进行编码,以便在clicked组件上触发releasedRectangle个事件?

其他信息:使用此代码,触摸第一项(索引:0),clickedreleased事件将被正确触发。但触摸其他项目我们只会收到canceled个事件。

0 个答案:

没有答案