From 56734cdf3383837f1fd59f2a0e8b0f39fcce71f3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 30 Sep 2016 11:53:07 +0200 Subject: QFileSystemModel: improve readability of the renaming code Instead of calling QHash::value(), inserting the return value into the hash with a different name and only many lines later removing the old node, do the extraction into a QScopedPointer, and the insertion into the hash from the QScopedPoiner, keeping the node update in between the two. Avoids the double-lookup of 'oldName', makes it clearer what's going on, and limits the potential for some return between the insertion under the new name and the removal under the old one sneaking in, which would cause a double-delete later in the dtor. Change-Id: Ia2d1cca77c04708421ccb5e594729ec83de85345 Reviewed-by: Edward Welbourne --- src/widgets/dialogs/qfilesystemmodel.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/widgets/dialogs/qfilesystemmodel.cpp') diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 6e1bee94c4..db1ce3fe0e 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -904,16 +904,14 @@ bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, in int visibleLocation = parentNode->visibleLocation(parentNode->children.value(indexNode->fileName)->fileName); parentNode->visibleChildren.removeAt(visibleLocation); - QFileSystemModelPrivate::QFileSystemNode * oldValue = parentNode->children.value(oldName); - parentNode->children[newName] = oldValue; - oldValue->fileName = newName; - oldValue->parent = parentNode; + QScopedPointer nodeToRename(parentNode->children.take(oldName)); + nodeToRename->fileName = newName; + nodeToRename->parent = parentNode; #ifndef QT_NO_FILESYSTEMWATCHER - oldValue->populate(d->fileInfoGatherer.getInfo(QFileInfo(parentPath, newName))); + nodeToRename->populate(d->fileInfoGatherer.getInfo(QFileInfo(parentPath, newName))); #endif - oldValue->isVisible = true; - - parentNode->children.remove(oldName); + nodeToRename->isVisible = true; + parentNode->children[newName] = nodeToRename.take(); parentNode->visibleChildren.insert(visibleLocation, newName); d->delayedSort(); -- cgit v1.2.3