如何突出显示委托w.r.t FolderListModel的clicked(by mouse)元素?

时间:2015-02-18 08:49:52

标签: qml qtquick2

import QtQuick 2.0
import Qt.labs.folderlistmodel 2.0

Item
{
    Component {
        id: highlight
        Rectangle {
            id: rooot
            width: 180; height: 20
            color: ListView.isCurrentItem ? "black" : "red"; radius: 5

            y: list.currentItem.y
            Behavior on y {
                SpringAnimation {
                    spring: 3
                    damping: 0.2
                }
            }
        }
    }

    ListView {
        id: list
        width: 480; height: 400
        model: folderModel
        delegate: Text { id: h; text: fileName }

        highlight: highlight
        highlightFollowsCurrentItem: false

        focus: true
    }

    FolderListModel
    {
        id: folderModel
        folder:      "/home/anisha/"
        nameFilters: ["*"]
    }
}

仅当我使用键盘时才有效。如何让鼠标点击工作?

1 个答案:

答案 0 :(得分:3)

要对鼠标事件作出反应,您需要放置MouseArea项目。

在下面的示例中(作为您提供的代码的扩展版本),我在委托项目中添加了MouseArea,点击后将ListView的{​​{1}}设置为代表的currentIndexindex代表中可见的特殊属性。)

import QtQuick 2.0
import Qt.labs.folderlistmodel 2.0

Item
{
    Component {
        id: highlight
        Rectangle {
            id: rooot
            width: 180; height: 20
            color: ListView.isCurrentItem ? "black" : "red"; radius: 5

            y: list.currentItem.y
            Behavior on y {
                SpringAnimation {
                    spring: 3
                    damping: 0.2
                }
            }
        }
    }

    ListView {
        id: list
        width: 480; height: 400
        model: folderModel
        delegate:
        Text {
            id: h;
            text: fileName

            MouseArea {
                anchors.fill: parent
                onClicked: list.currentIndex = index
            }
        }

        highlight: highlight
        highlightFollowsCurrentItem: false

        focus: true
    }

    FolderListModel
    {
        id: folderModel
        folder:      "/home/anisha/"
        nameFilters: ["*"]
    }
}

作为替代方法,您可以尝试放置一个ListView填充整个MouseArea并使用ListView的{​​{1}}方法来检查单击了哪个代理。但是,在这种情况下,您需要关注更多边缘条件。

相关问题