Results 1 to 4 of 4

Thread: QListView and changing the hierarchy

  1. #1
    Join Date
    Jun 2007
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question QListView and changing the hierarchy

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListView and changing the hierarchy

    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.

  3. #3
    Join Date
    Jun 2007
    Posts
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QListView and changing the hierarchy

    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):
    Qt Code:
    1. iterq = QListViewItemIterator(xxxxxxxxx)
    2.  
    3. while iterq.current():
    4. item = iterq.current()
    5. if <some arbitrary condition>
    6. listView.takeItem( item)
    7. newparentItem.insertItem( item )
    8.  
    9. iterq += 1 # Bump the incrementor
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. iterq = QListViewItemIterator(xxxxxxxxx)
    2.  
    3. while iterq.current():
    4. item = iterq.current()
    5. if <some arbitrary condition>
    6. listView.takeItem( item)
    7. newparentItem.insertItem( item )
    8. continue #<-------******* ADDED! *****
    9.  
    10. iterq += 1 # Bump the incrementor
    To copy to clipboard, switch view to plain text mode 

    Hope this might help some other newbie some day!

    Sean
    Last edited by sgroarke; 13th June 2007 at 07:40. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QListView and changing the hierarchy

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.