实现对特定文件的监控,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. 0.o?让我看看怎么个事儿之SpringBoot自动配置

    学习 SpringBoot 自动配置之前我们需要一些前置知识点: Java注解,看完就会用 学会@ConfigurationProperties月薪过三千 不是银趴~是@Import! @Condit ...

  2. 【Logging 日志库】Cpp 日志库 boost::log 以及 glog 的对比

    日志能方便地诊断程序原因.统计程序运行数据,是大型软件系统必不可少的组件之一.本文将从设计上和功能上对比 C++ 语言常见的两款日志库: boost::log 和 google-glog . 设计 b ...

  3. C 与 C++ 区别

    C 与 C++ 区别 本文介绍 C 与 C++ 之间重要的或者容易忽略的区别.尽管 C++ 几乎是 C 的超集,C/C++ 代码混用一般也没什么问题,但是了解 C/C++ 间比较重要区别可以避免碰到一 ...

  4. 通过Navicate for MySQL导入SQL文件

    本文介绍通过Navicate for MySQL导入SQL文件的方法. 前提条件 已安装Navicat for MySQL 和 MySql.您可以前往 Navicat官网 下载Navicat for ...

  5. C#设计模式12——代理模式的写法

    1. 什么是代理模式? 代理模式是一种结构型设计模式,它允许通过代理对象来控制对真实对象的访问,以提供额外的功能或控制访问权限. 2. 代理模式的作用是什么? 代理模式可以为对象提供保护代理.远程代理 ...

  6. java基础-常用类-day11

    目录 1. 包装类 2. Integer类 3.util.date 4. java.sql.Date 5. SimpleDateFormat 6. Calendar 7. Math 8. String ...

  7. 修改elasticsearch默认索引返回数

    1. 背景 (1) 客户反映es查询只能返回10000个数据,而需求时返回1.9W个数据,因此需要设置对应索引的默认返回数index.max_result_window (2) 给客户部署的服务以do ...

  8. 2023第十四届极客大挑战 — PWN WP

    WP可能有点简陋,因为是直接从docx导入到博客的,实在不想再重新写了!大家凑合着看吧!哈哈哈,问题不大! pwn方向出自:队友 nc_pwntools 只要过了chal1和chal2即可执行任意命令 ...

  9. [转帖]直接内存监控不准确,netty玩了什么花?

    https://segmentfault.com/a/1190000044509636     是挺久没有"宠爱"我们netty小婊贝了,最近又开始搞事了. 于是,趁机探究了下MX ...

  10. [转帖]Linux 下rsync命令详细整理

    https://blog.csdn.net/weixin_44052462/article/details/116134761 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面 ...