소스 검색

Use iterator instead of foreach

Using QMutableListIterator instead of foreach loop, which is more efficient
and safer, especially since items might be removed during iteration.

Renamed variable treeItem to childItemObject.
Andreas Fetzer 10 년 전
부모
커밋
4a6aebf7e7
1개의 변경된 파일13개의 추가작업 그리고 8개의 파일을 삭제
  1. 13 8
      Libs/XNAT/Core/ctkXnatTreeModel.cpp

+ 13 - 8
Libs/XNAT/Core/ctkXnatTreeModel.cpp

@@ -246,7 +246,6 @@ void ctkXnatTreeModel::refresh(const QModelIndex& parent)
     // Force a fetch for current object (it might has changed on the server)
     xnatObject->fetch(true);
 
-    QList<ctkXnatObject*> children = xnatObject->children();
     int numChildren = rowCount(parent);
 
     bool addToTreeView (true);
@@ -255,28 +254,33 @@ void ctkXnatTreeModel::refresh(const QModelIndex& parent)
     // if not -> add them
     // For all items of the treeview, check if they are still on the server
     // if not -> remove them
-    foreach (ctkXnatObject* child, children)
+    QList<ctkXnatObject*> children = xnatObject->children();
+    QMutableListIterator<ctkXnatObject*> iter (children);
+
+    while (iter.hasNext())
     {
+      ctkXnatObject* child = iter.next();
       for (int i = 0; i < numChildren; ++i)
       {
-        ctkXnatObject* treeItem = item->child(i)->xnatObject();
+        ctkXnatObject* childItemObject = item->child(i)->xnatObject();
 
         // If the item was deleted from the server in the meantime
         // -> remove it from the treeview
-        if (!treeItem->exists())
+        if (!childItemObject->exists())
         {
           beginRemoveRows(parent, item->child(i)->row(), item->child(i)->row());
-          item->remove(treeItem);
+          item->remove(childItemObject);
           xnatObject->remove(child);
-          children.removeOne(child);
+          iter.remove();
           endRemoveRows();
           --numChildren;
+          --i;
           addToTreeView = false;
           break;
         }
 
-        if ((treeItem->id().length() != 0 && treeItem->id() == child->id()) ||
-            (treeItem->id().length() == 0 && treeItem->name() == child->name()))
+        if ((childItemObject->id().length() != 0 && childItemObject->id() == child->id()) ||
+            (childItemObject->id().length() == 0 && childItemObject->name() == child->name()))
         {
           addToTreeView = false;
           break;
@@ -295,6 +299,7 @@ void ctkXnatTreeModel::refresh(const QModelIndex& parent)
       addToTreeView = true;
     }
 
+    numChildren = rowCount(parent);
     for (int i=0; i<numChildren; i++)
     {
       refresh(index(i,0,parent));