[PyKDE] Remove ListViewItem item from ListView

Jonathan Gardner jgardn at alumni.washington.edu
Wed Mar 6 14:06:15 GMT 2002


On Wed, Mar 06, 2002 at 01:11:53PM +0100, Laurent Claustre wrote:
> Jonathan Gardner wrote:
> >On Tue, Mar 05, 2002 at 09:20:21AM +0100, Laurent Claustre wrote:
> >
> >>I'v got no help from my last e-mail about that, I know it can best
> >>concerne a Qt mailing-list, but maybe someone from you  has find a
> >>solution.  I don't know how to remove properly an item from Its
> >>ListView or ListViewItem parent.  It seems for the moment the only way
> >>is to clear the entire ListView and refill it afterward.  The ListView
> >>I manage can display a huge tree structure and removing/refilling the
> >>entire tree waste lot of cpu time.  Has anybody idea ?  Cheers.
> >>
> >
> >I'll take a stab at this. I have a widget that uses QListView and
> >QListViewItem. I just use the takeItem() function and let the variable
> >pass out of scope. I don't know if that is correct, but it seems to
> >work.
> >
>
> Ok,  but you tried with toplevel item or children item? I mean if you 
> remove an item which has a ListViewItem
> parent, the whole familly  is removed.
> 

Here's a fragment of my code. I'm not knee-deep in it right now, but I
did test everything so it works. You can download the code from
http://sf.net/bttotalcontrol/ and give it a whirl and see for yourself.
To get to this code, you have to set your vehicle to be an omnivehicle,
and play around with adding and removing items to an individual config.

class ConfigItem(QListViewItem): # {{{2

    # ...

    def sync_children(self): # {{{4
        """Get the child items in the order they are in with
        self.c.inv"""

        # Order them up...
        last = None
        for i in self.c.inv:
            it = self.find_item(i) # This is my function.
            if not it:
                it = VehicleItem(self, i) # This is a QListViewItem
            if last:
                # Move it to after last
                it.moveItem(last)
            else:
                # Move it to the top
                # I know, this is an ugly hack, but how else can you
                # move it to the top?
                self.takeItem(it)
                self.insertItem(it)
            last = it

        # Remove all the others
        if last:
            last = last.nextSibling()
        else:
            last = self.firstChild()
        while last:
            # THIS IS WHAT YOU ARE LOOKING FOR
            todel = last
            last = last.nextSibling()
            self.takeItem(todel)
            # todel should be garbage collected eventually
            # I'm lazy, I don't want to type "delete todel"

        # Add these guys in last of all. They are not part of
        # self.c.inv, but they should show up anyway.
        if self.c.power_weight:
            PowerItem(self, self.c)
        if self.c.heat_sink_weight:
            HeatSinkItem(self, self.c)

The last part is what you are looking for. Note that the parent of the
VehicleItem is a ConfigItem, which is a QListViewItem. The ConfigItem
does not disappear when I call self.takeItem() on one of its children.

Let's see your code one more time and compare notes.

Jonathan

PS. YOu should really add your comments to the end of a mail, especially
on a mailing list.





More information about the PyQt mailing list