函数
QFileSystemWatcher类用于监视指定目录文件夹/文件的重命名、移除、删除操作, 当发生这些变更时该类会发出信号。
常用函数如下:
- bool QFileSystemWatcher::addPath(const QString & path)
- void QFileSystemWatcher::directoryChanged(const QString & path)
- void QFileSystemWatcher::fileChanged(const QString & path)
释义
- 添加目录, 目录字符串中可以带有文件名及后缀。多次调用,可添加多个不同目录。
- 这是一个信号函数,当监视的文件夹包含的一级内容发生变更时,会发出此信号。
- 这是一个信号函数,当监视的文件发生内容改变重命名或被删除时,会发出此信号。
示例
QFileSystemWatcher * watch = new QFileSystemWatcher(this);
connect(watch, &QFileSystemWatcher::fileChanged, this, &Form::_changed);
watch->addPath("C:\\Users\\Administrator\\Documents\\untitled7\\a.txt");
_changed()信号函数:
#include <iostream>
void Form::_changed(const QString & path)
{
std::cout << "Form::_changed - " << qPrintable(path) << std::endl;
}
将目录C:\\Users\\Administrator\\Documents\\untitled7\\a.txt下的a.txt文件删除后,控制台打印出了如下信息: