Hi, A problem I've been having with implementing my model is that I needed to declare a root item initially so my model would be able to initialize with a valid parent. I implemented it like so :
class SceneGraphModel(QtCore.QAbstractItemModel):
def __init__(self, parent=None):
super(SceneGraphModel, self).__init__(parent)
self._rootNode=Node(None)
self.nodeRole = QtCore.Qt.UserRole
def rowCount(self, parent):
if not parent.isValid():
parentNode=self._rootNode
else:
parentNode = parent.internalPointer()
As you can see I created an empty Node Class object under the variable self._rootNode. Now I had no issues with being able to insert rows and remove them. However, it seems that declaring it initially as None is causing it to clash with my rowCount() method in my model class , first with the isValid() method and if I remove that. I will get a :
AttributeError: 'NoneType' object has no attribute 'childCount'
I'm assuming from this error that declaring it as None initially means it doesn't have any attributes which the model class seems to have problems dealing with. And even after inserting some rows it still has no valid childCount to access. Below is my Node class:
class Node(object):
def __init__(self, fullname, parent=None):
self._fullName=fullname
self._children=[]
self._parent=parent
if parent is not None:
parent.addChild(self)
def addChild(self, child):
self._children.append(child)
def insertChild(self, position, child):
if position < 0 or position > len(self._children):
return False
self._children.insert(position, child)
child._parent = self
return True
def removeChild(self, position):
if position < 0 or position > len(self._children):
return False
child = self._children.pop(position)
child._parent = None
return True
def name(self):
return self._name
def setName(self,name):
self._name=name
def child(self, row):
return self._children[row]
def childCount(self):
return len(self._children)
def parent(self):
return self._parent
def row(self):
if self._parent is not None:
return self._parent._children.index(self)
Is there a way of implementing this root node? So far the examples I've managed to find all seems to initialize the model with information already inserted and do not start off with an empty model. Is this supposed to be a design limitation of PyQT or am I just doing this wrong?
Thank you
Gann Boon Bay