Qt之QFileSystemWatcher
简述
QFileSystemWatcher类用于提供监视文件和目录修改的接口。
QFileSystemWatcher通过监控指定路径的列表,监视文件系统中文件和目录的变更。
调用addPath()函数可以监控一个特定的文件或目录。如果需要监控多个路径,可以使用addPaths()。通过使用removePath()和removePaths()函数来移除现有路径。
QFileSystemWatcher检查添加到它的每个路径,已添加到QFileSystemWatcher的文件可以使用的files()函数进行访问,目录则使用directories()函数进行访问。
当一个文件被修改、重命名或从磁盘上删除时,会发出fileChanged()信号。同样,当一个目录或它的内容被修改或删除时,会发射directoryChanged()信号。需要注意:文件一旦被重命名或从硬盘删除,目录一旦从磁盘上删除,QFileSystemWatcher将停止监控。
注:监控文件和目录进行修改的行为会消耗系统资源。这意味着,你的进程同时监控会有文件数量的限制。一些系统限制打开的文件描述符的数量默认为256。也就是说,如果你的进程试使用addPath()和addPaths()函数添加超过256个文件或目录到文件系统将会失败。
公共函数
bool addPath(const QString & path)
如果路径存在,则添加至文件系统监控,如果路径不存在或者已经被监控了,那么不添加。如果路径是一个目录,内容被修改或删除时,会发射directoryChanged()信号;否则,当文件被修改、重命名或从磁盘上删除时,会发出fileChanged()信号。
如果监控成功,返回true;否则,返回false.
监控失败的原因通常依赖于系统,但也包括资源不存在、接入失败、或总的监控数量限制等原因。
QStringList addPaths(const QStringList & paths)
添加每一个路径至添加至文件系统监控,如果路径不存在或者已经被监控了,那么不添加。返回值是不能被监控的路径列表。
QStringList directories() const
返回一个被监控的目录路径列表。QStringList files() const
返回一个被监控的文件路径列表。bool removePath(const QString & path)
从文件系统监控中删除指定的路径。如果监控被成功移除,返回true。删除失败的原因通常是与系统相关,但可能是由于路径已经被删除。
QStringList removePaths(const QStringList & paths)
从文件系统监控中删除指定的路径。返回值是一个无法删除成功的路径列表。
信号
- void directoryChanged(const QString & path)
当目录被修改(例如:在指定的路径中添加或删除一个文件)或从磁盘删除时,这个信号被发射。注意:如果有在短时间内有几种变化,可能有些变化不会发出这个信号。然而,变化的序列中的最后的变化总会发射这个信号。
注意:这是一个私有信号,可以用于信号连接但不能由用户发出。
- void fileChanged(const QString & path)
当在指定路径中的文件被修改、重命名或从磁盘上删除时,这个信号被发射。
注意:这是一个私有信号,可以用于信号连接但不能由用户发出。
示例
下面,我们来实现一个文件/目录监控的类。
FileSystemWatcher.h
#ifndef FILE_SYSTEM_WATCHER_H
#define FILE_SYSTEM_WATCHER_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 // FILE_SYSTEM_WATCHER_H
FileSystemWatcher.cpp
#include <QDir>
#include <QFileInfo>
#include <qDebug>
#include "FileSystemWatcher.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);
}
注释很详细,我就不过多讲解了,下面我们就可以使用了。
#include "FileSystemWatcher.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
FileSystemWatcher::addWatchPath("E:/Test");
....
return a.exec();
}
可以通过FileSystemWatcher::addWatchPath()来监控指定的文件/目录,监控之后,就可以在对应的路径进行更新测试了。
Qt之QFileSystemWatcher的更多相关文章
- [转]Qt 之 QFileSystemWatcher
简述 QFileSystemWatcher类用于提供监视文件和目录修改的接口. QFileSystemWatcher通过监控指定路径的列表,监视文件系统中文件和目录的变更. 调用addPath()函数 ...
- 《Qt 实战一二三》
简介 "我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流",不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的 ...
- Qt 文件监视器 QFileSystemWatcher
之前有过对Qt的QFile以Text纯文本方式进行读取时的学习,这两天由于实时需要又对QFileSystemWatcher(这个类是干什么用的)进行了学习,发现也是问题很让人头疼. 我想监视一个文件夹 ...
- Qt 文件处理(readLine可以读取char[],并且有qSetFieldWidth qSetPadChar 等全局函数)
Qt 文件处理 Qt提供了QFile类来进行文件处理,为了更方便地处理文本文件或二进制文件,Qt还提了QTextStream类和QDataStream类,处理临时文件可以使用QTemporaryFil ...
- Qt 学习之路:QFileSystemModel
上一章我们详细了解了QStringListModel.本章我们将再来介绍另外一个内置模型:QFileSystemModel.看起来,QFileSystemModel比QStringListModel要 ...
- qt model/view 架构自定义模型之QFileSystemModel
# -*- coding: utf-8 -*- # python:2.x #QFileSystemModel """ Qt 内置了两种模型:QStandardItemM ...
- 将vim作为QT开发的IDE
转载请注明链接与作者huihui1988 用了一段时间的vim,喜欢上了这种简洁高效的编辑器.恰逢正在学习QT中,于是将vim变成了开发QT的工具.以下是具体配置. 一.语法高亮支持: 1.打开VIM ...
- 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)
Use Case: 两个程序共享同一个Configuration文件,当一个程序作出改变的时候,需要另外一个程序能够及时响应. 之前其实猜的八九不离十,估计是有一个Timer,然后定时查询Config ...
- 14.QT-QFile文件,QBuffer缓冲区,QDir目录,QFileSystemWatcher文件系统监视
QFile Qt中所有与IO相关的类都继承于QIODevice,继承图如下所示: 其中QFile类便是用于文件操作的类 在QT中,将文件当做一种特殊的外部设备对待(比如:串口,usb等就是外部设备) ...
随机推荐
- Linux Mysql 1130错误解决
今天在win32下通过navicat 远程登录Mysql时出现如下错误: 想都不用想,肯定是Mysql的访问权限问题. 首先,通过终端(我用的是SSH)远程登录到Linux服务器,为了 ...
- linux bash: sqlplus: command not found 错误处理
在oracle用户下 ,执行sqlplus命令,抛出如上错误. 解决办法: 1.su oracle 2.cd /home/oracle 3. 执行命令 source .bash_pro ...
- 最新版Intel HD4000 桌面右键菜单去除方法
网上找了一圈都提示找不到指定模块,后来发现它换dll了 regsvr32 /u igfxDTCM.dll
- Swift - 自动布局库SnapKit的使用详解3(约束优先级,约束做动画)
1,约束优先级我们使用SnapKit的时候,还可以定义约束的优先级.这样当约束出现冲突的时候,优先级高的约束覆盖优先级低的约束.具体优先级可以放在约束链的结束处. (1)可以设置如下几种优先级 pri ...
- 胡扯两句——CDQ分治
之前听大神讲过CDQ分治大概是个什么东西,但是一直还没有真正去搞过.今天稍微看了一下,写点自己的理解. 首先CDQ分治有两个条件. 条件1:可以分成两个独立互不影响的问题(这里的"独立&qu ...
- POJ 2528 区间染色,求染色数目,离散化
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 47905 Accepted: 13903 ...
- [windows驱动]内核态驱动架构
1.windows驱动简介: 1.1 windows组件简介: 1.2 windows驱动类型: windows驱动分为两种基本类型: 用户态驱动在用户态下执行.它们一般提供一套win32应用程序和内 ...
- 一张图告诉你为什么 Gmail 是最好的邮箱,以及大量私货
今天早上,我的同事详细介绍了 Gmail 相比其他邮箱的优势,比如强大的垃圾邮件过滤.简单的使用界面.强大的功能设置等等.但是对我来说,这些并不是我使用 Gmail 的最重要原因. 我第一个正式的邮箱 ...
- 记录一些容易忘记的属性 -- UIKeyboard
//UIKeyboardWillShowNotification这个通知在软键盘弹出时由系统发送 //UIKeyboardWillShowNotification 通知:键盘将要显示的通知 ...
- beanUtil
mvc中,页面传值进来,struts2框架是用modeldriven spingmvc是model 不用框架的话,要手动一个一个的设置,然后在用dao方法与数据库联系 servlet框架有BeanUt ...