在QML PathView中的SetRootIndex [英] SetRootIndex in QML PathView

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

问题描述

我使用一个QML PathView 来显示我的模型。此类模型继承自 QStandardItemModel ,并且具有两个级别的数据(父项目和子项目)。我需要在PathView中显示模型的第二级,即所选父级的所有子级。使用 QAbstractItemView 这个结果可以通过使用 setRootIndex 函数来实现。如何使用 PathView



实现相同的结果有人可以帮助我吗?
提前感谢。



这里有一个模型示例:

  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);`在这里输入代码
// -----
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 。引用文档:


通常不需要创建DelegateModel。但是,当将QAbstractItemModel子类用作模型时,可用于操作和访问modelIndex。此外,DelegateModel与Package一起使用以提供多个视图的委托,并使用DelegateModelGroup来对委托项进行排序和过滤。


类型具有属性 rootIndex 。再次引用文档:


QAbstractItemModel提供了一个分层的数据树,而QML只对列表数据进行操作。 rootIndex允许由此模型提供QAbstractItemModel中的任何节点的子


属性,您需要设置(和重置),如链接文档的示例中所述。注意,通过使用 DelegateModel ,您的 PathView 中的委托不应该被定义。一个工作示例( visualdatamodel / slideshow.qml )在标准框架分发中的路径下可用:

  Qt / QtXXX / Examples / Qt-5.4 / quick / views 

最后请注意 DelegateModel VisualDataModel 通常以可互换的方式使用,因为


类型( VisualDataModel )由Qt QML模块提供由于兼容性原因。 现在,相同的实现主要在Qt QML模型模块中作为DelegateModel提供。



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.

Here a model example:

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 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:

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.

Such QML type has a property rootIndex. Quoting again the documentation:

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. rootIndex allows the children of any node in a QAbstractItemModel to be provided by this model.

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

Finally note that DelegateModel and VisualDataModel are often used in an interchangeable way since

This type (VisualDataModel) is provided by the Qt QML module due to compatibility reasons. The same implementation is now primarily available as DelegateModel in the Qt QML Models module.

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

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