PDA

View Full Version : Searching a hierarchical QStandardItemModel



Zoober7
14th March 2011, 23:37
I've been struggling for about an hour trying to figure out how to correctly use findItem to find an item in a heirarchical QStandardItemModel. The item is in column 2, so I set the column and I told it to search recursively (also tried MatchWrap too), and yet it returns nothing. I'm thinking this has something to do with the fact that only the first column in my table knows about the hierarchy, as you append rows to an item to create a hierarchical pattern, not the entire row. I told it also to search collumn 0 since that is the collumn that the hierarchy is built up in, but that failed, as expected, because I'm sure it did a search down just all the items in column 0.

So now I don't know what to try. How am I ever supposed to search for an item that isn't in the first collumn or at the top level of the hierarchy? Is there a better way I can build my QStandardItemModel such that it supports what I want to do? It currently is being displayed in a QTreeView, and looks great, but behind the scenes I need to be able to search the model.

Suggestions?

wysota
15th March 2011, 00:06
Unfortunately squidge has broken his crystal ball again so you'll have to provide some code for us to look at.

Zoober7
15th March 2011, 17:01
I got a successful bug in and confirmed by the Qt development team with the exact same description I gave before, but if you guys can think of another work around other than the one suggested let me know:

Bug Report (http://bugreports.qt.nokia.com/browse/QTBUG-18148)

wysota
15th March 2011, 21:08
Why do you think this is a bug? The docs clearly state only the data in a particular column is searched. If the parent doesn't satisfy the column condition, its children will not be searched. You can of course implement it by subclassing match but for a big model with lots of children this function will be terribly slow as it will have to traverse each index in the tree because it might happen that deep deep inside the tree there is a child that satisfies this condition. If you had 1000 rows with 1000 columns having 1000 rows with 1000 columns as children you would have to search through 10^12 items which would probably last a couple of years or at least a couple of days. With the current implementation you only have to check 10^6 items. If you have a particular model that has some special features that simplifiy the search you can reimplement match() and use this additional knowledge to perform a full scan of the tree.

Zoober7
16th March 2011, 01:10
Well here's a couple reasons:
1) Because when you search a model "recursively" you expect it to find ANY item or sub item in the model that matches your search. I'm sorry you don't agree, but the expected behavior is that if anything in the column your searching for matches it, it should be returned.
2) A person should realize that you're going to get a slow operation if you're just willy-nilly searching a huge model like that recrusively, the performance gain granted from only doing a crummy job of searching the list does not merit the said crummy searching.
3) This type of iteration over every item in the list only needs to occur if you are searching recursively, and could be remidied by constructing different mechanisms for tracking children.
4) The data shouldn't just reside in that column once its been appended to a certain item, unless there is the option to append a row to another, because there could be MANY items in other columns that have now just been completely hidden from a search.
5) If you use this model, which the documentation recommends you do, for hierarchical data models that may or may not have multiple collumns, you probably want to be able to search for things besides things just the first column. (if the first column is where your hierarchy is built up)
6) A Qt Support representitive also thinks its a bug?

I already wrote my own version of the QStandardItemModel::findItems function that remedies the problem and only acts if it is called recursively. I'm not trying to cause any problems, but it is broken in the way it works right now.

vrushalid
21st June 2013, 09:45
Hi Zoober7,

Could you please explain a bit more on how you wrote your version of the QStandardItemModel::findItems ? I even tried 'match' function, which works same as findItems :(.

Any further pointers would really be appreciated.

Regards,
Vrushali