基本信息
源码名称:监视文件目录变化(qt源码)
源码大小:1.86KB
文件格式:.rar
开发语言:C/C++
更新时间:2020-07-16
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
文件目录监视的代码,可以监视目录的改变,包括删除,新建重命名
#include "fileio.h"
#include<QDir>
#include<QFileInfo>
#include<QDatetime>
#include<QTextStream>
#include<QDir>
#include<QFileSystemWatcher>
#include<QPushButton>
/*
* 文件监视QFileSystemWatcher
* addpath removepath
* directoryChanged fileChanged
* 监视整个路径,那就可以前后做比较,找出哪个文件发生了什么
*
*
*/
FileIo::FileIo(QWidget *parent)
: QWidget(parent)
{
QTextStream cout(stdout);
setWindowTitle("file system");
QFileSystemWatcher *pfile_watch=new QFileSystemWatcher;
path_str="E:/";
QDir dir(path_str);
pfile_watch->addPath(path_str);
currentDirSet=QSet<QString>::fromList(dir.entryList(QDir::Dirs|QDir::Files));
connect(pfile_watch,SIGNAL(directoryChanged(QString)),this,SLOT(direchange(QString)));
}
void FileIo::direchange(QString path)
{
QTextStream cout(stdout);
QDir dir(path_str);
QSet<QString> newDirSet=QSet<QString>::fromList(dir.entryList(QDir::Dirs|QDir::Files));
QStringList newFile = (newDirSet - currentDirSet).toList(); // 添加了文件
QStringList deleteFile = (currentDirSet - newDirSet).toList(); // 文件已被移除
currentDirSet=newDirSet; //更新储存的目录内容
if (!newFile.isEmpty() && !deleteFile.isEmpty())
{
// 文件/目录重命名
if ((newFile.count() == 1) && (deleteFile.count() == 1))
{
cout << QString("File Renamed from %1 to %2").arg(deleteFile.first()).arg(newFile.first())<<endl;
}
}
else
{
// 添加新文件/目录至Dir
if (!newFile.isEmpty())
{
cout << "New Files/Dirs added: " ;
foreach (QString file, newFile)
{
// 处理操作每个新文件....
cout<<file<<endl;
}
}
// 从Dir中删除文件/目录
if (!deleteFile.isEmpty())
{
cout << "Files/Dirs deleted: " ;
foreach(QString file, deleteFile)
{
// 处理操作每个被删除的文件....
cout<<file<<endl;
}
}
}
}
FileIo::~FileIo()
{
}