是否可以仅显示QML列表视图的某些索引? [英] Is it possible to show only certain indexes of a QML listview?
问题描述
是否可以在QML列表视图中仅显示某些索引或一系列索引?
Is it possible to show only certain indexes or a range of indexes in QML listviews?
我有一个列表模型,其中包含大量我在重用的信息.是否有可能仅显示索引5到8的列表?
I have a listmodel that has a bunch of info that I am reusing. Would it be possible to have a list showing, for example, only indices 5 to 8?
推荐答案
提出一种纯QML解决问题的方法将很有趣.当然,这不是不是短路径,但这是一个解决方案.
It would be interesting to present a pure QML approach to the problem. This is not the shorted path, for sure, but it is a solution.
该方法基于 DelegateModel
在models
QML模块中可用.它读入文档:
The approach is based on the DelegateModel
available in the models
QML module. It reads in the documentation:
DelegateModel类型封装了一个模型,并且该委托将 实例化模型中的项目.
The DelegateModel type encapsulates a model and the delegate that will be instantiated for items in the model.
通常不必创建DelegateModel.但是,它可以 在以下情况下可用于操纵和访问modelIndex QAbstractItemModel子类用作模型.另外, DelegateModel 与Package一起使用,以提供多个视图的委托, 和和 DelegateModelGroup 进行排序和过滤委托项目.
It is usually not necessary to create a DelegateModel. However, it can be useful for manipulating and accessing the modelIndex when a QAbstractItemModel subclass is used as the model. Also, DelegateModel is used together with Package to provide delegates to multiple views, and with DelegateModelGroup to sort and filter delegate items.
DelegateModel
实际上是一种功能强大的类型,具有很多功能(有关详细信息,请参见链接的文档). DelegateModel
的两个关键属性是 groups
和 filterOnGroup
.前者基本上是DelegateModelGroup
的列表,它定义要过滤或不过滤的项目.后者用于应用特定的过滤器,即只需将属性设置为所选组的名称,即可选择groups
中包含的特定DelegateModelGroup
.
DelegateModel
is really a powerful type with a lot of functionalities (see the linked documentation for details). Two key properties of DelegateModel
are groups
and filterOnGroup
. The former is basically a list of DelegateModelGroup
which defines the items to be filtered or not. The latter is used to apply a specific filter, i.e. choose a specific DelegateModelGroup
contained in groups
, by simply setting the property to the name of the chosen group.
请注意,对VisualDataModel
或DelegateModel
的引用是相同的,因为出于兼容性原因提供了第一个(对于VisualDataGroup
w.r.t. DelegateModelGroup
也是如此).
Note that referring to VisualDataModel
or DelegateModel
is the same since the first is provided for compatibility reasons (the same applies to VisualDataGroup
w.r.t. DelegateModelGroup
).
总而言之,可以通过以下方式在完整QML中过滤模型:
Summing up, it is possible to filter a model in full QML in this way:
- 创建
model
作为过滤模型的来源 - 将
model
馈送到VisualDataModel
/DelegateModel
- 定义一个
VisualDataGroup
/DelegateModelGroup
(或多个)-includeByDefault
设置为false
以避免自动添加原始模型中的所有项 - 定义用于填充组的策略
- 将
filterOnGroup
设置为选定的组 - 将视图模型设置为
VisualDataModel
模型
- Create a
model
as a source of filtered models - Feed the
model
to aVisualDataModel
/DelegateModel
- Define a
VisualDataGroup
/DelegateModelGroup
(or more than one) -includeByDefault
set tofalse
to avoid automatic addition of all items from the original model - Define policies to populate the groups
- Set
filterOnGroup
to the chosen group - Set the view model to the
VisualDataModel
model
在下一个示例中,为简单起见,我只在Component.onCompleted
事件处理程序期间填充了该组一次.如前所述,应该选择策略,这取决于特定的用例.
In the next example, for simplicity, I just populate the group once, during the Component.onCompleted
event handler. As said, policies should be chosen and that's depends on the specific use case.
在此示例中,仅将key
角色等于0
的项目添加到组key0
中,该组是ListView
中所示的项目.上述清单在代码中突出显示.
In the example only the items with key
role equal to 0
are added to the group key0
which is the one shown in the ListView
. The checklist described above is highlighted in the code.
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
ApplicationWindow {
title: qsTr("DelegateModel test")
width: 200
height: 350
visible: true
ListView {
id: displayListView
anchors.fill: parent
spacing: 5
//
model: displayDelegateModel // 6
}
ListModel { // 1
id: myModel
ListElement { vis: "One"; key: 0; }
ListElement { vis: "two"; key: 1; }
ListElement { vis: "Three"; key: 0; }
ListElement { vis: "Four"; key: 0; }
ListElement { vis: "Five"; key: 1; }
ListElement { vis: "Six"; key: 1; }
ListElement { vis: "Seven"; key: 0; }
}
VisualDataModel {
id: displayDelegateModel
delegate: Rectangle {
anchors.left: parent.left
anchors.right: parent.right
height: 25
color: "steelblue"
Text {
text: vis
anchors.centerIn: parent
font.bold: true
font.pixelSize: 20
}
}
model: myModel // 2
groups: [
VisualDataGroup { // 3
includeByDefault: false // NECESSARY TO AVOID AUTOADDITION
name: "key0"
}
]
filterOnGroup: "key0" // 5
Component.onCompleted: { // 4
var rowCount = myModel.count;
items.remove(0,rowCount);
for( var i = 0;i < rowCount;i++ ) {
var entry = myModel.get(i);
if(entry.key == 0) {
items.insert(entry, "key0");
}
}
}
}
}
这篇关于是否可以仅显示QML列表视图的某些索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!