问题描述
我正在使用 QML PathView
来显示我的模型.这样的模型继承自 QStandardItemModel
并具有两个级别的数据(父项和子项).我需要在 PathView 中显示模型的第二级,即选定父级的所有子级.使用 QAbstractItemView
这个结果可以通过使用来实现setRootIndex
函数.如何使用 PathView
获得相同的结果?
I'm using a QML PathView
to show my model. Such a model inherits from QStandardItemModel
and has two levels of data (parent items and child items). I need to show the second level of the model in the PathView, i.e. all the children of a selected parent. Using a QAbstractItemView
this result can be achieve by using the setRootIndex
function. How can I achieve the same result with a PathView
?
有人可以帮我吗?提前致谢.
Can someone help me?Thanks in advance.
这是一个模型示例:
newPetModel::newPetModel()
{
...
fillModel();
}
...
void newPetModel::fillModel()
{
QStandardItem* rootItem = invisibleRootItem();
// groups
QStandardItem* GroupAnimals = new QStandardItem();
rootItem->setChild(rootItem->rowCount(), GroupAnimals);
GroupAnimals->setData(QString("Animals"),nameRole);
QStandardItem* GroupPlants = new QStandardItem();
rootItem->setChild(rootItem->rowCount(), GroupPlants);
GroupPlants->setData(QString("Plants"),nameRole);
QStandardItem* GroupInsects = new QStandardItem();
rootItem->setChild(rootItem->rowCount(), GroupInsects);
GroupInsects->setData(QString("Insects"),nameRole);
// items
QStandardItem* Cat = new QStandardItem();
GroupAnimals->setChild(GroupAnimals->rowCount(), Cat);
Cat->setData(QString("Cat"),nameRole);
Cat->setData(QString("qrc:/cat.jpg"),imgRole);
QStandardItem* Dog = new QStandardItem();
GroupAnimals->setChild(GroupAnimals->rowCount(), Dog);
Dog->setData(QString("Dog"),nameRole);
Dog->setData("qrc:/dog.jpg",imgRole);`enter code here`
//-----
QStandardItem* Peas = new QStandardItem();
GroupPlants->setChild(GroupPlants->rowCount(), Peas);
Peas->setData(QString("Peas"),nameRole);
Peas->setData("qrc:/peas.jpg",imgRole);
//-----
QStandardItem* Spider = new QStandardItem();
GroupInsects->setChild(GroupInsects->rowCount(), Spider);
Spider->setData(QString("Spider"),nameRole);
Spider->setData("qrc:/peas.jpg",imgRole);
QStandardItem* Fly = new QStandardItem();
GroupInsects->setChild(GroupInsects->rowCount(), Fly);
Fly->setData(QString("Fly"),nameRole);
Fly->setData("qrc:/fly.jpg",imgRole);
}
推荐答案
QML 适用于列表模型,正如您在您的案例中所见.但是,使用 DelegateModel代码>
.引用文档:
QML works with list models, as you have seen also in your case. However, this limitation can be easily overcome by using DelegateModel
. Quoting the documentation:
通常不需要创建 DelegateModel.但是,当 QAbstractItemModel 子类用作模型时,它对于操作和访问模型索引很有用.此外,DelegateModel 与 Package 一起使用以向多个视图提供委托,并与 DelegateModelGroup 一起使用以对委托项进行排序和过滤.
这种QML类型有一个属性rootIndex
.再次引用文档:
Such QML type has a property rootIndex
. Quoting again the documentation:
QAbstractItemModel 提供了一个分层的数据树,而 QML 只对列表数据进行操作.rootIndex 允许此模型提供 QAbstractItemModel 中任何节点的子节点.
这是您需要设置(和重置)的属性,如链接文档示例中所述.请注意,通过使用 DelegateModel
,不应定义 PathView
中的委托.一个工作示例(visualdatamodel/slideshow.qml
)在路径下的标准框架分发中可用:
This is the property you need to set (and reset), as described in the example of the linked documentation. Note that by using DelegateModel
the delegate in your PathView
should not be defined. A working example (visualdatamodel/slideshow.qml
) is available in the standard framework distribution under the path:
Qt/QtXXX/Examples/Qt-5.4/quick/views
最后注意 DelegateModel
和 VisualDataModel
经常以可互换的方式使用,因为
Finally note that DelegateModel
and VisualDataModel
are often used in an interchangeable way since
由于兼容性原因,此类型 (VisualDataModel) 由 Qt QML 模块提供.相同的实现现在主要用作 Qt QML 模型中的 DelegateModel 模块.
这篇关于在 QML PathView 中设置根索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!