Qt::QFileSystemWatcher监控目录或文件的改变

这里只演示了目录,当目录下新增目录、文件,或修改文件名、目录名,删除目录和文件时,都会触发信号。记录一下怎么使用

参考:

Qt 之 QFileSystemWatcher_一去丶二三里的博客-CSDN博客_qfilesystemwatcher

代码

#pragma once

#include <QMainWindow>
#include<QFileSystemWatcher>
#include<QMap>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public:
    static void AddWatchPath(QString path);
public slots:
    ///
    /// \brief DirectoryUpdated 目录更新时调用,path 是监控的路径
    /// \param path
    ///
    void DirectoryUpdated(const QString & path);

    ///
    /// \brief FileUpdated 文件被修改时调用,path是监控的路径
    /// \param path
    ///
    void FileUpdated(const QString & path);
private:
    Ui::MainWindow *ui;
    static MainWindow * _Instance;
    QFileSystemWatcher * _FileWatcher;
    QMap<QString, QStringList> _CurrentContentsMao;//当前每个监控的目录列表
};
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include<QDebug>
#include <QDir>
#include<QFileInfo>
MainWindow * MainWindow ::_Instance = nullptr;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::AddWatchPath(QString path)
{
    qDebug() << QString("Add to watch: %1").arg(path);
    if(_Instance == nullptr)
    {
        _Instance = new MainWindow();
        _Instance->_FileWatcher = new QFileSystemWatcher();

        //连接信号槽
        connect(_Instance->_FileWatcher, &QFileSystemWatcher::directoryChanged, _Instance, &MainWindow::DirectoryUpdated) ;
        connect(_Instance->_FileWatcher, &QFileSystemWatcher::fileChanged, _Instance, &MainWindow::FileUpdated);
    }

    //添加监控路径
    _Instance->_FileWatcher->addPath(path);

    //如果添加路径是一个目录,保存当前内容列表
    QFileInfo file(path);
    if(file.isDir())
    {
        const QDir dirw(path);
        _Instance->_CurrentContentsMao[path] = dirw.entryList(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
    }
}

void MainWindow::DirectoryUpdated(const QString &path)
{
    qDebug() << QString("Directory updated: %1").arg(path);

    //比较最新的内容和保存的内容找出变化
    QStringList currEntryList = _CurrentContentsMao[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();

        // 更新当前设置
        _CurrentContentsMao[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 MainWindow::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 "MainWindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow::AddWatchPath("F:/CC");
    MainWindow w;
    w.show();
    return a.exec();
}

效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值