Is there a way to detect when a file copy process finish?
Is there a function or library Qt that say me when a copy file process finish?
This probably depends how you are copying the file.
If you use QFile::copy [doc.qt.nokia.com] it should tell you at the end of copy process, if successful or not. So, if it returns the copying shall have finished.
Well, that’s a good question then.
I assume QFileSystemWatcher uses the Windows API and so inherits its limitations. For example the Windows API is known to raise change events only when data is actually written to disk (data might reside in cache for a while).
As a temporary workaround I would set up a QTimer which monitors file size and as soon as the size is stable for a certain amount of time I would assume the operation is completed. You could an add additional check by trying to open the file for writing (should fail if file is still beeing copied).
You might raise a bugreport on JIRA [bugreports.qt.nokia.com] and see what the Qt devs think about it. Keep in mind that QFileSystemWatcher is considered deprecated due to flawed design. You might see an improved version of it in the near to mid future.
I had the same problem. My solution was to periodically check when the file was last modified and if it has not been modified in the last second I start reading it.
- void FileReader::tryToReadFileLoop()
- if(m_fileInfoList.at(m_positionInFileInfoList).lastModified() < QDateTime::currentDateTime().addSecs(-1))
- if(m_iLoopCount < 360)
- QTimer::singleShot(500, this, SLOT(tryToReadFileLoop()));
- m_iLoopCount += 1;
- //something went wrong file exists but could not be read
- emit fileCouldNotBeRead(m_sFilePath);
an even better solution would be if you implement different versions for the different OS, and use the OS specific methods, like for example “fuser” or “lsof” (this is for Linux) to see if the file is currently opened by another process.