C/C++ Qt 监控文件状态变化
实现对特定文件的监控,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 监控文件状态变化的更多相关文章
- .NET Core的文件系统[1]:读取并监控文件的变化
ASP.NET Core 具有很多针对文件读取的应用.比如我们倾向于采用JSON文件来定义配置,所以应用就会涉及针对配置文件读取.如果用户发送一个针对物理文件的HTTP请求,应用会根据指定的路径读取目 ...
- 使用FileSystemWatcher监控文件夹及文件
引言 这一周主要精力集中学习一个同事开发的本地文件搜索项目上,其中客户端添加共享文件时主要是使用FileSystemWatcher 监控文件,并在各种事件发生时向服务器发送消息. 解决方法 FileS ...
- Python 的 pyinotify 模块 监控文件夹和文件的变动
官方参考: https://github.com/seb-m/pyinotify/wiki/Events-types https://github.com/seb-m/pyinotify/wiki/I ...
- [转] 解析Qt资源文件使用
解析Qt资源文件使用 转自:http://mobile.51cto.com/symbian-270121.htm 本文详细的介绍了Qt文件的使用,和大部分GUI框架设计工具一样,Qt也引入了资源文件系 ...
- systemtap [主设备号,次设备好,inode]监控文件
SystemTap 是监控和跟踪运行中的linux 内核的操作的动态方法,SystemTap 应用:对管理员,SystemTap可用于监控系统性能,找出系统瓶颈,而对于开发者,可以查看他们的程序运行时 ...
- Qt之文件操作 QFile
原地址:http://blog.csdn.net/liuhongwei123888/article/details/6084761 今天学习QT的文件操作 1.QIODevice直接继承自QObjec ...
- QT 打开文件对话框汇总
Qstring fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ", tr( ...
- 利用pyinotify监控文件内容,像tailf命令但比它更强
Linux的tail/tailf命令使用了内核提供的inotify功能,下面的Python例子也使用inotify实现比tail/tailf更强的监控文件功能. watchfile.py #!/usr ...
- 根据 inotify 自己开发软件监控文件系统活动
了解 inotify Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除.读.写和卸载操作等.您还可以跟踪活动的源头和目标等细节. 使用 ...
- 【QT】文件读写操作
读取输出: QFile file("D:/Englishpath/QTprojects/1.dat"); if(!file.open(QIODevice::ReadOnly)) { ...
随机推荐
- Windows10/11 wsl2 安装 ArchLinux 子系统
这篇文章针对的是在win11系统的wsl2下安装ArchLinux系统,网上很多中文教程都是使用LxRunOffline去做的,但是实际上该方法已经过时了,目前有更加先进的ArchWSL方式. 如果用 ...
- Printer Queue,UVa 12100 (自定义标记法 + 优先队列)
题目描述: 我们需要用打印机打印任务.每个任务都有1~9间的优先级,优先级越高,任务越急. 打印机的运作方式:从打印队列里取出一个任务j,如果队列里有比j更急的任务,则直接把j放到打印队列尾部,否则打 ...
- Codeforces Round #658 (Div. 2)
A.Common Subsequence 题意 给你两组数,问你有没有相同 的书,有的话,输出最短的那组(大家都知道,1是最小的) AC #include<bits/stdc++.h> ...
- 【每日一题】36. 小AA的数列 (二进制DP)
补题链接:Here 算法涉及:位运算,DP 这道题想了很久但实在没想什么巧妙的解法,暴力的代码就不放,这里引用Kur1su 的思路 异或问题优先考虑二进制位,对于这个问题,我们需要考虑偶数长度的区间, ...
- CodeCraft-21 and Codeforces Round #711 (Div. 2) A~C 个人题解
补题链接:Here 1498A. GCD Sum 题意:给定一个 gcdSum 操作:\(gcdSum(762) = gcd(762,7 + 6 + 2) = gcd(762,15) = 3\) 请问 ...
- Tomcat 9.0.26 高并发场景下DeadLock问题排查与修复
本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/-OcCDI4L5GR8vVXSYhXJ7w作者:黄卫兵.陈锦霞 一.Tomcat容器 9.0. ...
- OpenSCA受邀出席2023 Open Compliance Summit
近日,由Linux基金会主办的2023 Open Compliance Summit(开放合规峰会,简称OCS)在日本东京隆重召开.悬镜安全旗下全球极客开源数字供应链安全社区OpenSCA受邀参与,O ...
- Linux mknod命令详解
Linux一切皆文件,系统与设备通信之前,要建立一个存放在/dev目录下的设备文件,默认情况下就已经生成了很多设备文件,有时候自己手动新建一些设备文件,这就会用到mknod. 语法格式:mknod[选 ...
- 密码加密bcrypt
- LLM面面观之Prefix LM vs Causal LM
1. 背景 关于Prefix LM和Causal LM的区别,本qiang在网上逛了一翻,发现多数客官只给出了结论,但对于懵懵的本qiang,结果仍是懵懵... 因此,消遣了多半天,从原理及出处,交出 ...