QML中ItemSelectionModel的用途及用法 [英] Purpose and usage of ItemSelectionModel in QML

查看:379
本文介绍了QML中ItemSelectionModel的用途及用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览 QML 文档时,我发现了这个值得称道的文档类:ItemSelectionModel

While skimming the QML Documentation, I found this commendable documented class: ItemSelectionModel

有 C++ 类 QItemSelectionModel它提供了一些关于跟踪模型中项目选择的目的的更多细节.

There is the C++-Class QItemSelectionModel which provides some more detail on it's purpose to keep track on the selection of Items within a model.

但是在 QML 方面,我对如何使用它一无所知.

However on the QML-Side I have not the slightes clue, on how to use it.

假设,我有这个 ListModel

ListModel {
    id: lm
    ListElement { value: 0 }
    ListElement { value: 0 }
    ListElement { value: 0 }
    ListElement { value: 0 }
    ListElement { value: 1 }
    ListElement { value: 1 }
    ListElement { value: 2 }
    ListElement { value: 2 }
    ListElement { value: 0 }
    ListElement { value: 0 }
    ListElement { value: 2 }
    ListElement { value: 2 }
}

现在我有一个 View,我可以在其中显示所有这个模型,还有一个我只想显示它的选择的第二个视图.所以我创建了一个 ItemSelectionModel 并从第一个视图的委托中调用它的 select 方法,这似乎根本没有效果.甚至 hasSelection 属性都懒得更改.

Now I have a View in which I display all of this model, and a second view in which I only want to display a selection of it. So I created a ItemSelectionModel and called the select-method of it from the delegates of the first view which seemed to have no effect at all. Not even the hasSelection-property bothered to change.

Repeater {
    model: lm
    delegate: Rectangle {
        property int row: Math.floor(index / 4)
        property int column: index % 4
        width: 100
        height: 100
        x: 100 * column
        y: 100 * row
        border.color: 'black'

        MouseArea {
            anchors.fill: parent
            onClicked: {
                ism.select(index, ItemSelectionModel.Select | ItemSelectionModel.Current)
                console.log(ism.hasSelection)
            }
        }
    }
}

ItemSelectionModel {
    id: ism
    model: lm
}

所以我想知道这个组件的目的是什么,它似乎什么都不做.或者,我怎样才能让它做一些有目的的事情?

So I wonder what is the purpose of this Component, that seems to do nothing at all. Or, how can I get it to do something purposeful?

推荐答案

文档确实没有任何帮助.抱歉,我已经提交了一个错误来研究修复它.

The documentation is indeed not at all helpful. Sorry for that, I've filed a bug to look into fixing it.

在 QML 世界中,它应该实现与 QItemSelectionModel 相同的功能(即保持多个视图的选择状态同步)——实际上,QML 中的实现是直接实例化和调用实际上和QItemSelectionModel的一样.

In the QML world, it is supposed to fulfill the same function as QItemSelectionModel (i.e. keeping the selection state of multiple views in sync) -- indeed, the implementation in QML is directly instantiating and calling is actually the same as QItemSelectionModel's.

这实际上可能是您遇到麻烦的根源,因为 QML 的视图不使用 QModelIndex(QItemSelectionModel 需要),而是使用 int index 指的是模型的行号.要获得 QModelIndex,您可以调用 QAbstractItemModel::index,如下所示:

This may actually be the source of your trouble, as QML's views do not use QModelIndex (which QItemSelectionModel requires), but rather, an int index referring to the row number of the model. To get a QModelIndex, you can call QAbstractItemModel::index, like so:

onClicked: {
    // note: lm here is the id of your ListModel
    ism.select(lm.index(index, 0), ItemSelectionModel.Select | ItemSelectionModel.Current)
    console.log(ism.selectedIndexes)
    console.log(ism.hasSelection)
}

这篇关于QML中ItemSelectionModel的用途及用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆