Results 1 to 5 of 5

Thread: PyQt Tree Model: move TreeItems

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2012
    Posts
    4
    Qt products
    Platforms
    Windows

    Default PyQt Tree Model: move TreeItems

    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)

    Qt Code:
    1. def moveTreeItem(self, treeItem, newParentTreeItem, newRow):
    2. oldParentTreeItem = treeItem.parentItem
    3. oldRow = oldParentTreeItem.childItems.index(treeItem)
    4.  
    5. #self.beginMoveRows(self.getIndex(oldParentTreeItem), oldRow, oldRow, self.getIndex(newParentTreeItem), newRow)
    6.  
    7. self.insertTreeItem(treeItem, newParentTreeItem, newRow)
    8. self.removeTreeItemByRow(oldParentTreeItem, oldRow)
    9.  
    10. #self.endMoveRows()
    11.  
    12. def insertTreeItem(self, childItem, parentItem, childRow=0):
    13. #insert a tree item as a new child of a parent item at a given row
    14. #childItem.parentItem = parentItem
    15. if childRow <= parentItem.childCount():
    16. parentIndex = self.getIndex(parentItem)
    17. self.beginInsertRows(parentIndex, childRow, childRow)
    18. parentItem.childItems.insert(childRow, childItem)
    19. self.endInsertRows()
    20. else:
    21. print "Error in insertTreeItem: row is larger row count: " + str(row) + " > " + str(parentItem.childCount())
    22.  
    23. def removeTreeItem(self, treeItem):
    24. parentItem = treeItem.parentItem
    25. row = treeItem.row()
    26. self.removeTreeItemByRow(parentItem, row)
    27.  
    28. def removeTreeItemByRow(self, parentItem, row):
    29. parentIndex = self.getIndex(parentItem)
    30. self.beginRemoveRows(parentIndex, row, row)
    31. del parentItem.childItems[row]
    32. self.endRemoveRows()
    To copy to clipboard, switch view to plain text mode 

    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
    Attached Files Attached Files
    Last edited by matameko; 26th November 2012 at 13:14.

Similar Threads

  1. SQL Tree Model Help
    By MTK358 in forum Newbie
    Replies: 9
    Last Post: 22nd June 2015, 15:02
  2. PyQt Busy progress Bar does not move
    By ilion_007 in forum Qt Programming
    Replies: 0
    Last Post: 10th May 2011, 01:33
  3. Writing a Tree model.
    By kaushal_gaurav in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2009, 11:22
  4. how to move between columns in tree view?
    By Scott Shiff in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2007, 08:01
  5. how to display full tree item name on mouse move ?
    By rajesh in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2006, 08:41

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
  •  
Qt is a trademark of The Qt Company.