[Solved]Find a Child in a QTreeView
-
wrote on 31 Mar 2011, 17:57 last edited by
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
-
wrote on 31 Mar 2011, 18:02 last edited by
you can iterate over all items of the model and check the value. Afaik, there is no find or search.
-
wrote on 31 Mar 2011, 18:07 last edited by
I see...i thought that it could be another way....thankss
-
wrote on 31 Mar 2011, 19:20 last edited by
As 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.
-
wrote on 31 Mar 2011, 20:22 last edited by
is QHash quicker?
-
wrote on 31 Mar 2011, 20:58 last edited by
As 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.
-
wrote on 31 Mar 2011, 21:03 last edited by
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...
-
wrote on 31 Mar 2011, 21:08 last edited by
[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. -
wrote on 31 Mar 2011, 21:20 last edited by
[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.
-
wrote on 31 Mar 2011, 21:35 last edited by
Well 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?
-
wrote on 31 Mar 2011, 21:41 last edited by
Yes, MatchRecursive makes the search look into the childs too.
-
wrote on 31 Mar 2011, 21:46 last edited by
Ok, i didn't know, thanks for the advice..
-
wrote on 31 Mar 2011, 22:08 last edited by
No problem, you're welcome - and that's what this forum is meant for :-)
Just let us know, if it works out for your case.
-
wrote on 1 Apr 2011, 18:12 last edited by
yep, it works! thanks again...
-
wrote on 1 Apr 2011, 18:40 last edited by
Again, you're welcome. You can mark the post as solved now: Just hit the edit link of the original post in this thread (it's located under your avatar right of the post) and prepend "[Solved]" to the title.
-
wrote on 1 Apr 2011, 18:45 last edited by
done!
1/16