实现对特定文件的监控,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();
}

C/C++ Qt 监控文件状态变化的更多相关文章

  1. .NET Core的文件系统[1]:读取并监控文件的变化

    ASP.NET Core 具有很多针对文件读取的应用.比如我们倾向于采用JSON文件来定义配置,所以应用就会涉及针对配置文件读取.如果用户发送一个针对物理文件的HTTP请求,应用会根据指定的路径读取目 ...

  2. 使用FileSystemWatcher监控文件夹及文件

    引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...

  3. Python 的 pyinotify 模块 监控文件夹和文件的变动

    官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...

  4. [转] 解析Qt资源文件使用

    解析Qt资源文件使用 转自:http://mobile.51cto.com/symbian-270121.htm 本文详细的介绍了Qt文件的使用,和大部分GUI框架设计工具一样,Qt也引入了资源文件系 ...

  5. systemtap [主设备号,次设备好,inode]监控文件

    SystemTap 是监控和跟踪运行中的linux 内核的操作的动态方法,SystemTap 应用:对管理员,SystemTap可用于监控系统性能,找出系统瓶颈,而对于开发者,可以查看他们的程序运行时 ...

  6. Qt之文件操作 QFile

    原地址:http://blog.csdn.net/liuhongwei123888/article/details/6084761 今天学习QT的文件操作 1.QIODevice直接继承自QObjec ...

  7. QT 打开文件对话框汇总

    Qstring fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ",  tr( ...

  8. 利用pyinotify监控文件内容,像tailf命令但比它更强

    Linux的tail/tailf命令使用了内核提供的inotify功能,下面的Python例子也使用inotify实现比tail/tailf更强的监控文件功能. watchfile.py #!/usr ...

  9. 根据 inotify 自己开发软件监控文件系统活动

    了解 inotify Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除.读.写和卸载操作等.您还可以跟踪活动的源头和目标等细节. 使用 ...

  10. 【QT】文件读写操作

    读取输出: QFile file("D:/Englishpath/QTprojects/1.dat"); if(!file.open(QIODevice::ReadOnly)) { ...

随机推荐

  1. 当谈论 React hook,我们究竟说的是什么?

    这个标题很大,但是落点很小,只是我,一个开发者在学习和使用 hooks 中的一点感受和总结. React hook 的由来 React hook 的由来,其实也可以看作是前端技术不断演进的结果. 在 ...

  2. Spring七种事务传播行为与五种事务隔离级别

    一.事务的传播行为:通过Propagation定义: <!-- 配置事务通知 --><tx:advice id="txAdvice" transaction-ma ...

  3. Vue大数据可视化(大屏展示)解决方案

    DataV:组件库基于Vue (React版) ,主要用于构建大屏(全屏)数据展示页面即数据可视化 官网地址: http://datav.jiaminghi.com/guide/#%E7%94%A8% ...

  4. uni-app阿里图标引用

    @font-face { font-family: "iconfont"; /* Project id 2566540 */ src: url('~@/static/fonts/i ...

  5. Qt大型工程开发技术选型PartFinal:CLR调用COM组件

    Qt大型工程开发技术选型PartFinal:CLR调用COM组件 这里其实没什么内容了,直接上代码吧,如下文所示: #pragma once #using <mscorlib.dll> u ...

  6. andriod sdk安装与使用

    一.进入以下网站下载 https://www.androiddevtools.cn/ 选择sdk工具-sdktools,这个工具比较好,可以通过SDK Manager下载到各种想要的包 有zip与ex ...

  7. 【RTOS】基于RTOS的降低功耗的策略

    RTOS中降低功耗的策略 Saving Power with an RTOS 介绍 随着绿色节能产品需求的增加,快速增长的移动设备,其电池寿命最受关注,设计者必须要考虑在其设计中如何最大限度的降低功耗 ...

  8. 【C++】在搞touchgfx时遇见了一个初始化列表顺序与类中定义不一致的问题,error:when initialized here [-Werror=reorder]

    在搞touchgfx时遇见了一个初始化列表顺序与类中定义不一致的问题,error:when initialized here [-Werror=reorder] 初始化列表顺序与类中定义顺序不一致错误 ...

  9. 【Linux】字符驱动之sysfs接口实现

    [来源]https://blog.csdn.net/qq_20553613/article/details/104556269

  10. Java-Enum常量特定方法

    OnJava8-Enum-常量特定方法 用枚举实现责任链模式 责任链(Chain Of Responsibility)设计模式先创建了一批用于解决目标问题的不同方法,然后将它们连成一条"链& ...