Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

c++ - SetRootIndex in QML 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.

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...