PDA

View Full Version : QListView and changing the hierarchy



sgroarke
12th June 2007, 14:17
I think this a newbie question - I'm new to QT anyway...

I'm writing in Python, although I strongly suspect the solution to my problem is generic QT rather than Python-specific, so feel free to answer in C++.

I have a populated QListView. All items are top-level, so there are no parents/children.

I want to walk down the QListView and based upon a condition make some entries children, although only ever one-generation deep.

For example, if I have 10 (top level) items in my list to start, I wish (for example) to set items 2 - 4 to all be children of 1, 6 to be a child of 5, 8 - 10 to be children of 7.

So I end up with 3 top level items: 1, 5 & 7.

I'm sure it's trivially easy but I just can't do it!

The obvious (seeming) solution was to use a QListViewItemIterator to walk down the list and selectively takeItem and then insertItem.

Yet when I do this things go very awry - I'm guessing that taking items out of the list over which you are running an iterator is causing confusion...? Anyway, I can't get it to work for me!

So if anyone could outline how to achieve it (Python ideal, but C++, pseudo code, etc. all appreciated!!) I'd be very grateful. I'm sure I'm not the first person to want to do it, but I've searched high and low and cannot find an answer.

Sean

wysota
12th June 2007, 15:57
Can we see your code? Based on your description the approach seems correct, but if it doesn't work, you probably have an incorrect code.

sgroarke
13th June 2007, 07:39
Hi. Was about to post the code but had a final hack at it first and I *think* I see the problem (i.e. the hole into which I was falling!) It's probably worth mentioning, and I'd appreciate if anyone could confirm that I've got it right too.

My wrong version had me doing the following (pythonish pseudo-code):


iterq = QListViewItemIterator(xxxxxxxxx)

while iterq.current():
item = iterq.current()
if <some arbitrary condition>
listView.takeItem( item)
newparentItem.insertItem( item )

iterq += 1 # Bump the incrementor

The problem seems to be down to me double-incrementing iterq unwittingly! When the condition is true, it seems like when I do the takeItem the iterq is AUTOMATICALLY incremented. Which does make sense when I think about it, but is a point I had missed.

So the fix to my problem is really easy: when I go through the takeItem/insertItem section I explicitly avoid doing the iterq += 1 and simply (in Python) "continue" back at the while - knowing iterq has already been bumped.

The fixed version just has the one extra line:


iterq = QListViewItemIterator(xxxxxxxxx)

while iterq.current():
item = iterq.current()
if <some arbitrary condition>
listView.takeItem( item)
newparentItem.insertItem( item )
continue #<-------******* ADDED! *****

iterq += 1 # Bump the incrementor


Hope this might help some other newbie some day!

Sean

wysota
13th June 2007, 07:51
Yes, this is correct. The iterator was not "incremented" anywhere behind the scene - it's just that when the current item was removed, the next one leaped into its place, thus the iterator started pointing to the next object.