实现对特定文件的监控,Qt中提供了QFileSystemWatcher
调用这个接口可以快速实现监控功能,当有文件发生变化是自动触发并输出文件具体信息。
filesystem.h
#ifndef FILESYSTEM_H #define FILESYSTEM_H #include <QObject> #include <QMap> #include <QFileSystemWatcher>
class FileSystemWatcher : public QObject { Q_OBJECT
public: static void addWatchPath(QString path);
public slots: void directoryUpdated(const QString &path); void fileUpdated(const QString &path);
private: explicit FileSystemWatcher(QObject *parent = 0);
private: static FileSystemWatcher *m_pInstance; QFileSystemWatcher *m_pSystemWatcher; QMap<QString, QStringList> m_currentContentsMap;
}; #endif
|
filesystem.cpp
#include <QDir> #include <QFileInfo> #include <qDebug> #include "filesystem.h"
FileSystemWatcher* FileSystemWatcher::m_pInstance = NULL;
FileSystemWatcher::FileSystemWatcher(QObject *parent) : QObject(parent) { }
void FileSystemWatcher::addWatchPath(QString path) { qDebug() << QString("Add to watch: %1").arg(path);
if (m_pInstance == NULL) { m_pInstance = new FileSystemWatcher(); m_pInstance->m_pSystemWatcher = new QFileSystemWatcher();
connect(m_pInstance->m_pSystemWatcher, SIGNAL(directoryChanged(QString)), m_pInstance, SLOT(directoryUpdated(QString))); connect(m_pInstance->m_pSystemWatcher, SIGNAL(fileChanged(QString)), m_pInstance, SLOT(fileUpdated(QString))); }
m_pInstance->m_pSystemWatcher->addPath(path);
QFileInfo file(path); if (file.isDir()) { const QDir dirw(path); m_pInstance->m_currentContentsMap[path] = dirw.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst); } }
void FileSystemWatcher::directoryUpdated(const QString &path) { qDebug() << QString("Directory updated: %1").arg(path);
QStringList currEntryList = m_currentContentsMap[path]; const QDir dir(path);
QStringList newEntryList = dir.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
QSet<QString> newDirSet = QSet<QString>::fromList(newEntryList); QSet<QString> currentDirSet = QSet<QString>::fromList(currEntryList);
QSet<QString> newFiles = newDirSet - currentDirSet; QStringList newFile = newFiles.toList();
QSet<QString> deletedFiles = currentDirSet - newDirSet; QStringList deleteFile = deletedFiles.toList();
m_currentContentsMap[path] = newEntryList;
if (!newFile.isEmpty() && !deleteFile.isEmpty()) { if ((newFile.count() == 1) && (deleteFile.count() == 1)) { qDebug() << QString("File Renamed from %1 to %2").arg(deleteFile.first()).arg(newFile.first()); } } else { if (!newFile.isEmpty()) { qDebug() << "New Files/Dirs added: " << newFile;
foreach (QString file, newFile) { } }
if (!deleteFile.isEmpty()) { qDebug() << "Files/Dirs deleted: " << deleteFile;
foreach(QString file, deleteFile) { } } } }
void FileSystemWatcher::fileUpdated(const QString &path) { QFileInfo file(path); QString strPath = file.absolutePath(); QString strName = file.fileName();
qDebug() << QString("The file %1 at path %2 is updated").arg(strName).arg(strPath); }
|
主调文件main.cpp
#include <QCoreApplication> #include "filesystem.h"
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); FileSystemWatcher::addWatchPath("c://test"); return a.exec(); }
|