以下是使用QT的QThread封装监控文件夹文件变化的类:
```cpp
// MyWatcherThread.h 文件
#ifndef MYWATCHERTHREAD_H
#define MYWATCHERTHREAD_H
#include <QObject>
#include <QThread>
#include <QFileSystemWatcher>
class MyWatcherThread : public QThread
{
Q_OBJECT
public:
explicit MyWatcherThread(const QString &folder, QObject *parent = nullptr);
~MyWatcherThread();
signals:
void fileChanged(const QString &file);
private:
void run() override;
QString m_folder;
QFileSystemWatcher m_watcher;
};
#endif // MYWATCHERTHREAD_H
// MyWatcherThread.cpp 文件
#include "MyWatcherThread.h"
MyWatcherThread::MyWatcherThread(const QString &folder, QObject *parent)
: QThread(parent), m_folder(folder)
{
}
MyWatcherThread::~MyWatcherThread()
{
quit();
wait();
}
void MyWatcherThread::run()
{
m_watcher.addPath(m_folder);
connect(&m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(emitFileChanged(QString)));
exec();
}
void MyWatcherThread::emitFileChanged(const QString &path)
{
emit fileChanged(path);
}
```
在主界面中创建MyWatcherThread对象,并连接fileChanged信号即可获取到文件变化的信息。例如:
```cpp
#include <QApplication>
#include "MyWatcherThread.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWatcherThread *watcherThread = new MyWatcherThread("监控的文件夹路径");
QObject::connect(watcherThread, SIGNAL(fileChanged(QString)), [](const QString &file){
qDebug() << "File changed: " << file;
});
watcherThread->start();
return a.exec();
}
```
注意需要在pro文件中加入QT += core,否则会报错。
以下是使用QT的QThread监控文件夹文件变化的代码示例:
```cpp
// MyWatcher.h 文件
#ifndef MYWATCHER_H
#define MYWATCHER_H#include <QObject>
#include <QThread>
#include <QFileSystemWatcher>class MyWatcher : public QObject
{
Q_OBJECTpublic:
explicit MyWatcher(QObject *parent = nullptr);
~MyWatcher();signals:
void fileChanged(const QString &file);private slots:
void slotDirectoryChanged(const QString &path);
void slotFileChanged(const QString &path);private:
QThread m_thread;
QFileSystemWatcher m_watcher;
};#endif // MYWATCHER_H
// MyWatcher.cpp 文件
#include "MyWatcher.h"MyWatcher::MyWatcher(QObject *parent)
: QObject(parent)
{
m_watcher.moveToThread(&m_thread);
connect(&m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(slotDirectoryChanged(QString)));
connect(&m_watcher, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
m_thread.start();
}MyWatcher::~MyWatcher()
{
m_thread.quit();
m_thread.wait();
}void MyWatcher::slotDirectoryChanged(const QString &path)
{
qDebug() << "Directory changed: " << path;
}void MyWatcher::slotFileChanged(const QString &path)
{
qDebug() << "File changed: " << path;
emit fileChanged(path);
}
```在主界面中创建MyWatcher对象,然后连接fileChanged信号即可获取到文件变化的信息。例如:
```cpp
#include <QApplication>
#include "MyWatcher.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);MyWatcher *watcher = new MyWatcher();
QObject::connect(watcher, SIGNAL(fileChanged(QString)), [](const QString &file){
qDebug() << "File changed: " << file;
});watcher->m_watcher.addPath("监控的文件夹路径");
return a.exec();
}
```注意需要在pro文件中加入QT += core,否则会报错。
下面是基于QT的QThread监控文件夹文件变化的代码:
```cpp
#include <QThread>
#include <QFileSystemWatcher>
#include <QDebug>
class FolderWatcher : public QThread
{
Q_OBJECT
public:
FolderWatcher(const QString& path, QObject *parent = nullptr)
: QThread(parent), m_path(path) {}
signals:
void fileChanged(const QString& filePath);
protected:
void run() override
{
QFileSystemWatcher watcher;
watcher.addPath(m_path);
while(!isInterruptionRequested())
{
if(watcher.directories().isEmpty())
watcher.addPath(m_path);
if(watcher.files().isEmpty())
watcher.addPath(m_path + "/*");
if(watcher.directories().isEmpty() || watcher.files().isEmpty())
continue;
watcher.waitForChanged(-1);
const QStringList files = watcher.files();
for(const QString& filePath: files)
{
if(!QFile::exists(filePath))
continue;
emit fileChanged(filePath);
}
}
}
private:
QString m_path;
};
```
这是一个基于 QThread 和 QFileSystemWatcher 的文件夹监控器,它会在文件夹中的文件发生变化时,通过信号 fileChanged 将变化的文件路径发送出来。
在使用这个类时,你可以这样写:
```cpp
FolderWatcher* watcher = new FolderWatcher("C:/MyFolder", this);
connect(watcher, &FolderWatcher::fileChanged, [=] (const QString& filePath) {
qInfo() << "File changed: " << filePath;
});
watcher->start();
```
在这个例子中,我们创建一个文件夹监控器,并监控目录 "C:/MyFolder"。当该文件夹下的文件发生变化时,通过 lambda 函数输出日志信息。