我正在使用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);`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
可以轻松克服此限制.引用文档:
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
.再次引用文档:
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):
Qt/QtXXX/Examples/Qt-5.4/quick/views
最后请注意,DelegateModel和VisualDataModel
通常以可互换的方式使用
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.
标签:c,qt,qml 来源: https://codeday.me/bug/20190928/1825814.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。