PDA

View Full Version : Qt needs a sub item tree iterator



mrknight
3rd November 2010, 02:02
Thinking to save time writing a recursive function that would process all siblings and children from a given level in a QTreeWidget, I created a QTreeWidgetItemIterator using the item I wanted the iterator to start from in the constructor. All worked wonderfully until it reached the end of that branch, where I would expect the iterator to finish. But, against all common sense, it stepped up to the parent of the original item and started processing its siblings. I've never come across an iterator that behaves this way, so i checked the source to see if my eyes were deceiving me. And wouldn't you know it; look what I found...

[qtreewidgetitemiterator.cpp, line: 300] // if we had no sibling walk up the parent and try the sibling of that

...obviously it was designed to behave like a list (rows) iterator rather than a heirachial (siblings and child branches) iterator.

Would it be too much to ask for an iterator to traverse a given level in a tree, with the option to process child items? Or how about methods to access the next/prev sibling without having to access the QModelIndex of the current item?

mrknight
3rd November 2010, 04:18
For those that are interested, the trick to traversing sibling items is to check if the parents are the same.

if((*it)->parent() == item->parent())