問題描述
我正在使用 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 一起使用以對委托項進行排序和過濾.
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.
這種QML類型有一個屬性rootIndex
.再次引用文檔:
Such QML type has a property rootIndex
. Quoting again the documentation:
QAbstractItemModel 提供了一個分層的數據樹,而 QML 只對列表數據進行操作.rootIndex 允許此模型提供 QAbstractItemModel 中任何節點的子節點.
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.
這是您需要設置(和重置)的屬性,如鏈接文檔示例中所述.請注意,通過使用 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 模塊.
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 中設置根索引的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!