网站首页 > 博客文章 正文
实现对特定文件的监控,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); // 目录更新时调用,path是监控的路径
void fileUpdated(const QString &path); // 文件被修改时调用,path是监控的路径
private:
explicit FileSystemWatcher(QObject *parent = 0);
private:
static FileSystemWatcher *m_pInstance; // 单例
QFileSystemWatcher *m_pSystemWatcher; // QFileSystemWatcher变量
QMap<QString, QStringList> m_currentContentsMap; // 当前每个监控的内容目录列表
};
#endif // FILESYSTEM_H
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();
// 连接QFileSystemWatcher的directoryChanged和fileChanged信号到相应的槽
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
{
// 添加新文件/目录至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 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();
}
猜你喜欢
- 2024-09-08 Qt 贪吃蛇制作(含源码)(qt编写贪吃蛇)
- 2024-09-08 qt 提示“启动程序失败,路径或者权限错误?”解决方法
- 2024-09-08 电脑文件不小心删除了怎么办?两个方法教你秒恢复
- 2024-09-08 Qt——内存回收(qti内存)
- 2024-09-08 C++跨平台库QT学习 操作Excel(跨平台c++开发工具)
- 2024-09-08 Qt 工程 pro文件(qt工程文件.pro在哪儿找)
- 2024-09-08 Qt中的快捷键汇总(qtcreator快捷键)
- 2024-09-08 Qt中文乱码解决思路(vscode终端中文乱码怎么解决)
- 2024-09-08 Qt5+VS2015编程实例:下拉列表框QComboBox控件使用
- 2024-09-08 Qt编程进阶(87):基于HTTP协议的网络文件下载
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)