这里只演示了目录,当目录下新增目录、文件,或修改文件名、目录名,删除目录和文件时,都会触发信号。记录一下怎么使用
参考:
Qt 之 QFileSystemWatcher_一去丶二三里的博客-CSDN博客_qfilesystemwatcher
代码
#pragma once
#include <QMainWindow>
#include<QFileSystemWatcher>
#include<QMap>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public:
static void AddWatchPath(QString path);
public slots:
///
/// \brief DirectoryUpdated 目录更新时调用,path 是监控的路径
/// \param path
///
void DirectoryUpdated(const QString & path);
///
/// \brief FileUpdated 文件被修改时调用,path是监控的路径
/// \param path
///
void FileUpdated(const QString & path);
private:
Ui::MainWindow *ui;
static MainWindow * _Instance;
QFileSystemWatcher * _FileWatcher;
QMap<QString, QStringList> _CurrentContentsMao;//当前每个监控的目录列表
};
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include<QDebug>
#include <QDir>
#include<QFileInfo>
MainWindow * MainWindow ::_Instance = nullptr;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::AddWatchPath(QString path)
{
qDebug() << QString("Add to watch: %1").arg(path);
if(_Instance == nullptr)
{
_Instance = new MainWindow();
_Instance->_FileWatcher = new QFileSystemWatcher();
//连接信号槽
connect(_Instance->_FileWatcher, &QFileSystemWatcher::directoryChanged, _Instance, &MainWindow::DirectoryUpdated) ;
connect(_Instance->_FileWatcher, &QFileSystemWatcher::fileChanged, _Instance, &MainWindow::FileUpdated);
}
//添加监控路径
_Instance->_FileWatcher->addPath(path);
//如果添加路径是一个目录,保存当前内容列表
QFileInfo file(path);
if(file.isDir())
{
const QDir dirw(path);
_Instance->_CurrentContentsMao[path] = dirw.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
}
}
void MainWindow::DirectoryUpdated(const QString &path)
{
qDebug() << QString("Directory updated: %1").arg(path);
//比较最新的内容和保存的内容找出变化
QStringList currEntryList = _CurrentContentsMao[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();
// 更新当前设置
_CurrentContentsMao[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
{
// 添加新文件/目录至Dir
if (!newFile.isEmpty())
{
qDebug() << "New Files/Dirs added: " << newFile;
foreach (QString file, newFile)
{
// 处理操作每个新文件....
}
}
// 从Dir中删除文件/目录
if (!deleteFile.isEmpty())
{
qDebug() << "Files/Dirs deleted: " << deleteFile;
foreach(QString file, deleteFile)
{
// 处理操作每个被删除的文件....
}
}
}
}
void MainWindow::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 "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow::AddWatchPath("F:/CC");
MainWindow w;
w.show();
return a.exec();
}
效果: