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:
QModelIndexList list = selectedIndexes();
RosterSortProxy *proxy = dynamic_cast<RosterSortProxy*>(model());
RosterItemModel *itemModel = dynamic_cast<RosterItemModel*>(proxy->sourceModel());
QString group
= action
->property
("group").
toString();
if(index.isValid()) {
RosterContact *cnt = static_cast<RosterContact*>(index.internalPointer());
if(cnt) {
itemModel->moveToGroup(cnt, group);
}
}
}
proxy->invalidate();
QModelIndexList list = selectedIndexes();
RosterSortProxy *proxy = dynamic_cast<RosterSortProxy*>(model());
RosterItemModel *itemModel = dynamic_cast<RosterItemModel*>(proxy->sourceModel());
QString group = action->property("group").toString();
foreach(QModelIndex in, list) {
QModelIndex index = proxy->mapToSource(in);
if(index.isValid()) {
RosterContact *cnt = static_cast<RosterContact*>(index.internalPointer());
if(cnt) {
itemModel->moveToGroup(cnt, group);
}
}
}
proxy->invalidate();
To copy to clipboard, switch view to plain text mode
moveToGroup:
RosterItem *newGroup = groupItem(groupName);
RosterItem *oldGroup = groupItem(item->contact()->group());
if(newGroup != oldGroup) {
oldGroup->removeChild(item);
newGroup->appendChild(item);
if((oldGroup->childCount() == 0) && (oldGroup != m_root)) {
removeGroup(oldGroup);
}
}
RosterItem *newGroup = groupItem(groupName);
RosterItem *oldGroup = groupItem(item->contact()->group());
if(newGroup != oldGroup) {
oldGroup->removeChild(item);
newGroup->appendChild(item);
if((oldGroup->childCount() == 0) && (oldGroup != m_root)) {
removeGroup(oldGroup);
}
}
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.
Bookmarks