matameko
26th November 2012, 13:05
Hello,
I have a document class that contains several "Ikwed Objects". Each "Ikwed Object" can have several child "Ikwed Objects".
The document has a tree model, that is used to show the hierarchy of the "Ikwed Objects" in a tee view.
I am able to create new "Ikwed Objects" in the document, append or insert childs and also to remove childs in the tree model.
However, If I try to move a "child tree item" to the root branch, I get errors in the tree view or infite loops that I do not understand.
For example I want to move the child IkwedObject11 to the start of the root branch:
-IkwedObject0
-IkwedObject1
++ IkwedObject11
++ IkwedObject12
=>
-IkwedObject11
-IkwedObject0
-IkwedObject1
++ IkwedObject12
Could you please have a look at the function "moveTreeItem" of the attached example and give me hints how to adapt it?
I wrote the example with Spyder 2.1.9 (PythonXY on Windows Platform, with QT 4.9)
def moveTreeItem(self, treeItem, newParentTreeItem, newRow):
oldParentTreeItem = treeItem.parentItem
oldRow = oldParentTreeItem.childItems.index(treeItem)
#self.beginMoveRows(self.getIndex(oldParentTreeIte m), oldRow, oldRow, self.getIndex(newParentTreeItem), newRow)
self.insertTreeItem(treeItem, newParentTreeItem, newRow)
self.removeTreeItemByRow(oldParentTreeItem, oldRow)
#self.endMoveRows()
def insertTreeItem(self, childItem, parentItem, childRow=0):
#insert a tree item as a new child of a parent item at a given row
#childItem.parentItem = parentItem
if childRow <= parentItem.childCount():
parentIndex = self.getIndex(parentItem)
self.beginInsertRows(parentIndex, childRow, childRow)
parentItem.childItems.insert(childRow, childItem)
self.endInsertRows()
else:
print "Error in insertTreeItem: row is larger row count: " + str(row) + " > " + str(parentItem.childCount())
def removeTreeItem(self, treeItem):
parentItem = treeItem.parentItem
row = treeItem.row()
self.removeTreeItemByRow(parentItem, row)
def removeTreeItemByRow(self, parentItem, row):
parentIndex = self.getIndex(parentItem)
self.beginRemoveRows(parentIndex, row, row)
del parentItem.childItems[row]
self.endRemoveRows()
The code creats a default document and displays it. Please comment the line 345
treeModel.moveTreeItem(treeObj11, rootItem, 0)
at the end of the init function of TestWidget to see the model without moved items.
The wrong result I get is:
-IkwedObject12
-IkwedObject0
-IkwedObject1
++ IkwedObject12
Why is the first item "IkwedObject12" instead of "IkwedObject11" ??!!
Sunny regards,
Stefan
I have a document class that contains several "Ikwed Objects". Each "Ikwed Object" can have several child "Ikwed Objects".
The document has a tree model, that is used to show the hierarchy of the "Ikwed Objects" in a tee view.
I am able to create new "Ikwed Objects" in the document, append or insert childs and also to remove childs in the tree model.
However, If I try to move a "child tree item" to the root branch, I get errors in the tree view or infite loops that I do not understand.
For example I want to move the child IkwedObject11 to the start of the root branch:
-IkwedObject0
-IkwedObject1
++ IkwedObject11
++ IkwedObject12
=>
-IkwedObject11
-IkwedObject0
-IkwedObject1
++ IkwedObject12
Could you please have a look at the function "moveTreeItem" of the attached example and give me hints how to adapt it?
I wrote the example with Spyder 2.1.9 (PythonXY on Windows Platform, with QT 4.9)
def moveTreeItem(self, treeItem, newParentTreeItem, newRow):
oldParentTreeItem = treeItem.parentItem
oldRow = oldParentTreeItem.childItems.index(treeItem)
#self.beginMoveRows(self.getIndex(oldParentTreeIte m), oldRow, oldRow, self.getIndex(newParentTreeItem), newRow)
self.insertTreeItem(treeItem, newParentTreeItem, newRow)
self.removeTreeItemByRow(oldParentTreeItem, oldRow)
#self.endMoveRows()
def insertTreeItem(self, childItem, parentItem, childRow=0):
#insert a tree item as a new child of a parent item at a given row
#childItem.parentItem = parentItem
if childRow <= parentItem.childCount():
parentIndex = self.getIndex(parentItem)
self.beginInsertRows(parentIndex, childRow, childRow)
parentItem.childItems.insert(childRow, childItem)
self.endInsertRows()
else:
print "Error in insertTreeItem: row is larger row count: " + str(row) + " > " + str(parentItem.childCount())
def removeTreeItem(self, treeItem):
parentItem = treeItem.parentItem
row = treeItem.row()
self.removeTreeItemByRow(parentItem, row)
def removeTreeItemByRow(self, parentItem, row):
parentIndex = self.getIndex(parentItem)
self.beginRemoveRows(parentIndex, row, row)
del parentItem.childItems[row]
self.endRemoveRows()
The code creats a default document and displays it. Please comment the line 345
treeModel.moveTreeItem(treeObj11, rootItem, 0)
at the end of the init function of TestWidget to see the model without moved items.
The wrong result I get is:
-IkwedObject12
-IkwedObject0
-IkwedObject1
++ IkwedObject12
Why is the first item "IkwedObject12" instead of "IkwedObject11" ??!!
Sunny regards,
Stefan