[Solved]Find a Child in a QTreeView
来源:互联网 发布:行知学院宿舍有空调吗 编辑:程序博客网 时间:2024/04/28 13:20
Hi everyone,
Anyone can help me how to find a child in QTreeView. I have a QTreeView with one standardItem and that standardItem has many childs, i would like to find a child but by the Qt::DisplayRole or Qt::UserRole. I Have seen the doc. of the QStandardItem and it seems that the only way to find a child its by "row" and "column"...
Thanks in advance
JETG
postedReputation: 1 | Posts: 76you can iterate over all items of the model and check the value. Afaik, there is no find or search.
Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)postedReputation: 87 | Posts: 3278I see...i thought that it could be another way....thankss
JETG
postedReputation: 1 | Posts: 76As Gerolf said you'll need to iterate over the item hierarchy yourself. If you find yourself needing to do this many times, then I would consider constructing an index object that allows you to quickly lookup the needed items based upon the roles that you need to search upon.
Something like QMap<QVariant, QStandardItem*> might be useful and will allow you to do lookups of items in O(log(n)) rather than O(n) times. Of course it will be up to you to maintain this index as your data changes.
Nokia Certified Qt Specialist
Interested in hearing about Qt related workpostedReputation: 40 | Posts: 1453is QHash quicker?
JETG
postedReputation: 1 | Posts: 76As it seems that you use a "QStandardItemModel":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html, have you tried "QStandardItemModel::findItems() ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#findItems already?
If you need more control you can call "QAbstractItemModel::match() ":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#match. The latter returns only a QModelIndexList, you will have to peek the items with "QStandardItemModel::itemFromIndex() ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#itemFromIndex then.
http://www.catb.org/~esr/faqs/smart-questions.html
postedReputation: 38 | Posts: 5422Hi volker,
Well i have tried with findItems (and match), but the standarditemmodel only has the root items not the childs....I had to iterate just as Gerolf says...
JETG
postedReputation: 1 | Posts: 76[quote author="gronerth" date="1301602957"]is QHash quicker?[/quote]
Possibly but it does not store the items in an ordered manner which is sometimes useful to have if you need to iterate over them in some specific order. Depends upon the exact use case you wish to cater for.Nokia Certified Qt Specialist
Interested in hearing about Qt related workpostedReputation: 40 | Posts: 1453[quote author="gronerth" date="1301605423"]Hi volker,
Well i have tried with findItems (and match), but the standarditemmodel only has the root items not the childs....I had to iterate just as Gerolf says...[/quote]
How do you create a hierarchy then? And what model do you use?
You can add Qt::MatchRecursive to the search flags of match and findItems.
http://www.catb.org/~esr/faqs/smart-questions.html
postedReputation: 38 | Posts: 5422Well the item root:
@
QStandardItem *root = new QStandardItem("rootItem");
model->setItem(0,root);
@The childs items
@
root->setChild(row,device2Add);
@I haven't trie with Qt::MatchRecursive, does it search too in the childs of each item?
JETG
postedReputation: 1 | Posts: 76Yes, MatchRecursive makes the search look into the childs too.
http://www.catb.org/~esr/faqs/smart-questions.html
postedReputation: 38 | Posts: 5422
- [Solved]Find a Child in a QTreeView
- A problem that solved in a formal method-Modelchecking
- How to find child controls that are located in the template of a parent control
- solved a problem about undefined / unresolved reference in c
- Find a Mother Vertex in a Graph
- How to initialize child controls in a derived CDialogBar
- How to initialize child controls in a derived CDialogBar
- A child who is lost in his life
- JESUS TO A CHILD
- Have a first child
- A Problem I Solved Last Night
- Is Query Optimization a “Solved” Problem?
- Find a useful sentence in YzLib
- find a job in HK(urls)
- Find missing number in a sorted array
- [Amazon] Find loop in a singly linkedlist
- Find first repeating letter in a String
- 438. Find All Anagrams in a String
- php 调用 mongodb
- CSS3 3D旋转动画代码实例
- java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layou
- JVM优化相关博客帖汇总
- iOS开发笔记22--使用XIB自定义一个UIView
- [Solved]Find a Child in a QTreeView
- CCFlow6的公文审批、公文审批单以及公文审批流程
- 好!欢迎你来我院工作!
- 一些高效的Linux命令行操作 (转自君淋天下的博客)
- POJ 2796 Feel Good
- 实现对一个8bit数据的指定位置0或置1操作,其他位不变
- Android面试题
- git http/https方式储存密码
- JAVA分布式事务原理及应用(转)