[PyKDE] [PyQt]: how to refresh a QTreeView widget?

Andreas Pakulat apaku at gmx.de
Fri Sep 8 16:23:57 BST 2006


On 08.09.06 16:29:32, Oscar Cossu wrote:
>    def child(self, i):
>        if self.childItems.has_key(i):
>            return self.childItems[i]
> 
>        if i >= 0 and i < self.domNode.childNodes().count():
>            childNode = self.domNode.childNodes().item(i)
>            childItem = DomItem(childNode, i, self)
>            self.childItems[i] = childItem
>            return childItem

I think the problem lies here, for caching you put the child nodes into
a dict, but in the removeRows function, you never update this dict. Now
when a row is removed the tree thinks there are only 2 childs left and
thus always asks for items 0 and 1. Due to the dict still containing a
reference to the DomItem and the underlying QDomElement you return the
wrong element.

>    def findNodeByTagNameID(self,tagName,id):
>        element = self.domDocument.elementsByTagName(tagName)
>        for e in  range(element.length()) :
>            tmp=element.item(e).attributes().namedItem("id").nodeValue()
>            if  tmp==id:
>                return element.item(e)
> 
>        return None
> 
>    def removeRows(self,arow,count,parent):
>          print "removeRow"
>          self.beginRemoveRows(parent,arow,arow+count-1)
>          for ii in range(count):
>              nodoIndex=self.index(arow+ii,0,parent)
>              NomeNodo= nodoIndex.data().toString()
>              IdNodo=self.index(arow+ii,1,parent).data().toString()
> 
>              if IdNodo.contains("id"):
>                  i=IdNodo.indexOf("id")
>                  i=IdNodo.indexOf("\"",i)
>                  f=IdNodo.indexOf("\"",i+1)
>                  IdNodo=IdNodo.mid(i+1,f-i-1)
>              else:
>                  return False
> 
>              nodo=self.findNodeByTagNameID(NomeNodo,IdNodo)
>              nodo.parentNode().removeChild(nodo)

Why do you make it so complicated? You have a row and the parent. Just
use something like

parentdomitem = index(parent.internalPointer().row(),0, parent.parent()).internalPointer()
childdomitem = parentdomitem.child(arow+ii)
childdomitem.node.parentNode().removeChild(childdomitem.node)
del parentdomitem.childItems[arow+ii]

I think that should do it inside the for loop.

Andreas

-- 
Green light in A.M. for new projects.  Red light in P.M. for traffic tickets.




More information about the PyQt mailing list