是它可以显示QML的ListView只有某些指标? [英] is it possible to show only certain indexes of QML listview?
问题描述
是否有可能只显示某些索引或者QML列表视图一系列指标?
Is it possible to show only certain indexes or a range of indexes in QML listviews?
我有了一堆信息,我确信重用ListModel的。有没有可能有一个列表显示例如只指数5〜8?
i have a listmodel that has a bunch of info that i am reusing. Would it be possible to have a list show for example only index 5 to 8?
推荐答案
这将是有趣的present纯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
在型号
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
被一起使用与包装提供委托给多个视图,
和 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
的两个关键特性的 组
和的 filterOnGroup
。前者是基本上 DelegateModelGroup
的列表定义待过滤与否的项目。后者是用于应用特定的过滤器,即选择一个特定的 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
WRT 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:
- 创建一个
模式
作为过滤模型的来源 - 订阅
模式
到VisualDataModel
/DelegateModel
- 定义
VisualDataGroup
/DelegateModelGroup
(或多个) -includeByDefault
设置为假
来避免自动除全部从原来的模型项目 - 定义的政策来填充群组
- 设置
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 a policies to populate the group(s)
- 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.
在这个例子只能用键
作用等于 0
添加到组<$ C $项目C> KEY0 这是在的ListView
所示。上述清单中的code高亮显示。
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的ListView只有某些指标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!