@stampede: Well, if dir.entryList() returns the list of files in alphabetical order, that's one advantage over using the QSet if you want to display the files to the user. QSet stores entries in whatever order the internal hashing determines, so iterating over the contents of the QSet will not return an alphabetical list, even if they were inserted in alphabetical order. If you wanted to display the current contents in alphabetical order, you'd have to copy the QSet into a QStringList, then sort the list. This isn't so bad, since QString instances with the same values are shared.
So let's use the best of both our ideas - keep the current directory contents as a list, but use sets to determine what has changed:
void MyClass
::directoryChanged( const QString & path
) {
QSet<QString> newDirSet = QSet<QString>::fromList( newEntryList );
QSet<QString> currentDirSet = QSet<QString>::fromList( _currentEntryList );
// Files that haven't changed
QSet<QString> sameFiles = currentDirSet & newDirSet;
// Files that have been added
QSet<QString> newFiles = newDirSet - currentDirSet;
// Files that have been removed
QSet<QString> deletedFiles = currentDirSet - newDirSet;
// and update the current set
_currentEntryList = newEntryList;
}
void MyClass::directoryChanged( const QString & path )
{
const QDir dir(path);
QStringList newEntryList = dir.entryList();
QSet<QString> newDirSet = QSet<QString>::fromList( newEntryList );
QSet<QString> currentDirSet = QSet<QString>::fromList( _currentEntryList );
// Files that haven't changed
QSet<QString> sameFiles = currentDirSet & newDirSet;
// Files that have been added
QSet<QString> newFiles = newDirSet - currentDirSet;
// Files that have been removed
QSet<QString> deletedFiles = currentDirSet - newDirSet;
// and update the current set
_currentEntryList = newEntryList;
}
To copy to clipboard, switch view to plain text mode
Bookmarks