| 1 |
/*! |
| 2 |
@~chinese |
| 3 |
@ingroup dfilesystem |
| 4 |
@file include/filesystem/dfilewatchermanager.h |
| 5 |
|
| 6 |
@class DFileWatcherManager |
| 7 |
@brief DFileWatcherManager 类可以帮助管理一系列 DFileWatcher 文件监视器,并在文件变动时发送信号通知. |
| 8 |
@details |
| 9 |
示例代码: |
| 10 |
```cpp |
| 11 |
|
| 12 |
#include "dfilewatchermanager" |
| 13 |
#include <QCoreApplication> |
| 14 |
#include <QTemporaryFile> |
| 15 |
#include <QDebug> |
| 16 |
#include <QTimer> |
| 17 |
DCORE_USE_NAMESPACE |
| 18 |
|
| 19 |
int main(int argc, char **argv) |
| 20 |
{ |
| 21 |
QCoreApplication app(argc, argv); |
| 22 |
DFileWatcherManager manager; |
| 23 |
QTemporaryFile tmpfile1; // 创建临时文件1 |
| 24 |
tmpfile1.open(); |
| 25 |
QFile file1( tmpfile1.fileName()); |
| 26 |
QTemporaryFile tmpfile2; // 创建临时文件2 |
| 27 |
tmpfile2.open(); |
| 28 |
QFile file2( tmpfile2.fileName()); |
| 29 |
|
| 30 |
manager.add(tmpfile1.fileName());// 监控临时文件1 |
| 31 |
manager.add(tmpfile2.fileName());// 监控临时文件2 |
| 32 |
|
| 33 |
QObject::connect(&manager, &Dtk::Core::DFileWatcherManager::fileModified, &app, [=](const QString value) { |
| 34 |
qDebug() << "文件发生变动:" << value; |
| 35 |
}); // 文件发生变动时打印文件路径 |
| 36 |
|
| 37 |
QObject::connect(&manager, &Dtk::Core::DFileWatcherManager::fileDeleted, &app, [=](const QString value) { |
| 38 |
qDebug() << "文件被删除:" << value; |
| 39 |
}); |
| 40 |
|
| 41 |
|
| 42 |
file1.open(QIODevice::WriteOnly|QIODevice::Text);// 修改临时文件1 |
| 43 |
file1.write("test"); |
| 44 |
file1.close(); |
| 45 |
|
| 46 |
file2.open(QIODevice::WriteOnly|QIODevice::Text);// 修改临时文件2 |
| 47 |
file2.write("test"); |
| 48 |
file2.close(); |
| 49 |
|
| 50 |
qDebug() << manager.watchedFiles();// 打印所有被监控的文件路径 |
| 51 |
qDebug() << "---------------------------"; |
| 52 |
app.processEvents();// 处理事件 |
| 53 |
manager.removeAll();// 移除所有的监控 |
| 54 |
qDebug() << manager.watchedFiles();// 打印所有被监控的文件路径 |
| 55 |
return app.exec(); |
| 56 |
} |
| 57 |
``` |
| 58 |
上面代码演示了如何使用 DFileWatcherManager 类来监控文件变动和清除文件变动的监控. |
| 59 |
具体可以参照源码中的example文件夹中的文件变动监控例子. |
| 60 |
|
| 61 |
@fn DFileWatcher* DFileWatcherManager::add(const QString &filePath) |
| 62 |
@brief 为路径`filePath`创建 DFileWatcher 并将其添加到 DFileWatcherManager 中. |
| 63 |
@return 被创建并添加到 DFileWatcherManager 的 DFileWatcher |
| 64 |
|
| 65 |
@fn void DFileWatcherManager::remove(const QString &filePath) |
| 66 |
@brief 从 DFileWatcherManager 中移除路径`filePath`对应的 DFileWatcher. |
| 67 |
|
| 68 |
@fn void DFileWatcherManager::removeAll() |
| 69 |
@brief 从 DFileWatcherManager 中移除所有的 DFileWatcher. |
| 70 |
|
| 71 |
@fn void DFileWatcherManager::watchedFiles() |
| 72 |
@brief 获取 DFileWatcherManager 中所有的 DFileWatcher. |
| 73 |
|
| 74 |
@var void DFileWatcherManager::fileDeleted(const QString &filePath) |
| 75 |
@brief 当路径`filePath`对应的文件被删除时发送此信号. |
| 76 |
|
| 77 |
@var void DFileWatcherManager::fileAttributeChanged(const QString &filePath) |
| 78 |
@brief 当路径`filePath`对应的文件属性发生变化时发送此信号. |
| 79 |
|
| 80 |
@var void DFileWatcherManager::fileMoved(const QString &fromFilePath, const QString &toFilePath) |
| 81 |
@brief 当路径`fromFilePath`对应的文件被移动到路径`toFilePath`时发送此信号. |
| 82 |
|
| 83 |
@var void DFileWatcherManager::fileClosed(const QString &filePath) |
| 84 |
@brief 当路径`filePath`对应的文件被关闭时发送此信号. |
| 85 |
|
| 86 |
@var void DFileWatcherManager::fileModified(const QString &filePath) |
| 87 |
@brief 当路径`filePath`对应的文件被修改时发送此信号. |
| 88 |
|
| 89 |
@var void DFileWatcherManager::subfileCreated(const QString &filePath) |
| 90 |
@brief 当路径`filePath`对应的文件夹中有新的子文件被创建时发送此信号. |
| 91 |
|
| 92 |
*/ |