I made a treeview with a model based on this example http://doc.qt.nokia.com/latest/itemv...treemodel.html And now I want to move items from one branch to another. Here's some of my code:

Qt Code:
  1. QModelIndexList list = selectedIndexes();
  2. RosterSortProxy *proxy = dynamic_cast<RosterSortProxy*>(model());
  3. RosterItemModel *itemModel = dynamic_cast<RosterItemModel*>(proxy->sourceModel());
  4.  
  5. QString group = action->property("group").toString();
  6.  
  7. foreach(QModelIndex in, list) {
  8. QModelIndex index = proxy->mapToSource(in);
  9.  
  10. if(index.isValid()) {
  11. RosterContact *cnt = static_cast<RosterContact*>(index.internalPointer());
  12. if(cnt) {
  13. itemModel->moveToGroup(cnt, group);
  14. }
  15. }
  16. }
  17.  
  18. proxy->invalidate();
To copy to clipboard, switch view to plain text mode 

moveToGroup:
Qt Code:
  1. RosterItem *newGroup = groupItem(groupName);
  2. RosterItem *oldGroup = groupItem(item->contact()->group());
  3.  
  4. if(newGroup != oldGroup) {
  5. oldGroup->removeChild(item);
  6. newGroup->appendChild(item);
  7.  
  8. if((oldGroup->childCount() == 0) && (oldGroup != m_root)) {
  9. removeGroup(oldGroup);
  10. }
  11. }
To copy to clipboard, switch view to plain text mode 

removeChild and appendChild just remove/append the item to group's list of children.

And the problem I'm having is sometimes when I try to move a item, the program crashes, I tried to find a pattern for those crashes but couldn't. Also sometimes not all selected items are moved. I think it's an issue with the model not being updated, but I tried layoutAboutToBeChanged() and beginMoveRows() I even tried using both beginRemoveRows() and beginInsertRows() with no luck, I don't know if I'm using them wrong or what.

Please help.