QFileSystemWatcher 类

QFileSystemWatcher 类提供用于监视修改文件 目录的接口。 更多...

头: #include <QFileSystemWatcher>
qmake: QT += core
Since: Qt 4.2
继承: QObject

该类在 Qt 4.2 引入。

注意: 此类的所有函数 可重入 .

公共函数

QFileSystemWatcher (const QStringList & paths , QObject * parent = nullptr)
QFileSystemWatcher (QObject * parent = nullptr)
virtual ~QFileSystemWatcher ()
bool addPath (const QString & path )
QStringList addPaths (const QStringList & paths )
QStringList directories () const
QStringList 文件 () const
bool removePath (const QString & path )
QStringList removePaths (const QStringList & paths )

信号

void directoryChanged (const QString & path )
void fileChanged (const QString & path )

详细描述

QFileSystemWatcher 通过看守指定路径的列表,监视文件系统对文件 目录的改变。

调用 addPath () 以看守特定文件或目录。可以添加多个路径使用 addPaths () 函数。可以移除现有路径通过使用 removePath () 和 removePaths () 函数。

QFileSystemWatcher 审查添加到它的每个路径。可以访问已添加到 QFileSystemWatcher 的文件使用 文件 () 函数,和目录使用 directories () 函数。

fileChanged () 信号被发射当文件被修改、被重命名或从磁盘被移除时。同样, directoryChanged () 信号被发射当目录或其内容被修改或删除时。注意:文件一旦被重命名或从磁盘被移除 (目录一旦从磁盘被移除) QFileSystemWatcher 就会停止监视。

  • 注意事项 :
    • 在不支持 inotify 的运行 Linux 内核的系统,无法取消挂载包含看守路径的文件系统。
    • 监视文件和目录修改的行动会消耗系统资源。这隐含进程可以同时监视文件和目录的数量是受到限制的。例如,在所有 BSD 变体,每个受监视文件均要求打开文件描述符。某些系统把打开文件描述符数限制为 256,默认情况下。这意味着 addPath () 和 addPaths () 会失败若进程试着向文件系统监视器添加超过 256 个文件或目录。另请注意:进程可能有其它打开文件描述符除要监视的文件外,且这些其它打开描述符也计入总数。macOS 使用不同后端,因此不会遭受此问题。

另请参阅 QFile and QDir .

成员函数文档编制

QFileSystemWatcher:: QFileSystemWatcher (const QStringList & paths , QObject * parent = nullptr)

构造新的文件系统看守程序对象采用给定 parent which monitors the specified paths 列表。

QFileSystemWatcher:: QFileSystemWatcher ( QObject * parent = nullptr)

构造新的文件系统看守程序对象采用给定 parent .

[signal] void QFileSystemWatcher:: directoryChanged (const QString & path )

This signal is emitted when the directory at a specified path is modified (e.g., when a file is added or deleted) or removed from disk. Note that if there are several changes during a short period of time, some of the changes might not emit this signal. However, the last change in the sequence of changes will always generate this signal.

注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。

另请参阅 fileChanged ().

[signal] void QFileSystemWatcher:: fileChanged (const QString & path )

此信号被发射当文件在指定 path 被修改、被重命名或从磁盘被移除。

注意: 作为安全措施,许多应用程序通过写入新文件然后删除旧文件来保存打开文件。在槽函数中,可以校验 watcher.files().contains(path) 。若它返回 false ,校验文件是否仍然存在,然后调用 addPath() 以继续看守它。

注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。

另请参阅 directoryChanged ().

[virtual] QFileSystemWatcher:: ~QFileSystemWatcher ()

销毁文件系统看守程序。

bool QFileSystemWatcher:: addPath (const QString & path )

添加 path 到文件系统看守程序若 path 存在。路径不被添加若路径不存在 (或路径已被文件系统看守程序所监视)。

path 指定目录, directoryChanged () 信号会被发射当 path 被修改或从磁盘被移除;否则 fileChanged () 信号被发射当 path 被修改、被重命名或被移除。

若看守成功,返回 true。

看守失败的原因通常从属系统,但可能包括:资源不存在、访问失败或总看守计数限制 (若平台有限制的话)。

注意: There may be a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit is been reached, path will not be monitored, and false is returned.

另请参阅 addPaths () 和 removePath ().

QStringList QFileSystemWatcher:: addPaths (const QStringList & paths )

添加每个路径在 paths 到文件系统看守程序。路径不被添加若路径不存在 (或路径已被文件系统看守程序所监视)。

若路径指定目录, directoryChanged () 信号会被发射当路径被修改或从磁盘被移除时;否则 fileChanged () 信号被发射当路径被修改、被重命名或被移除时。

返回值是无法看守的路径的列表。

看守失败的原因通常从属系统,但可能包括:资源不存在、访问失败或总看守计数限制 (若平台有限制的话)。

注意: There may be a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit has been reached, the excess paths will not be monitored, and they will be added to the returned QStringList .

另请参阅 addPath () 和 removePaths ().

QStringList QFileSystemWatcher:: directories () const

返回正被看守的目录路径列表。

另请参阅 文件 ().

QStringList QFileSystemWatcher:: 文件 () const

返回正被看守的文件路径列表。

另请参阅 directories ().

bool QFileSystemWatcher:: removePath (const QString & path )

移除指定 path 从文件系统看守程序。

若看守被成功移除,返回 true。

移除看守失败的原因通常从属系统 (如:可能由于路径已被删除)。

另请参阅 removePaths () 和 addPath ().

QStringList QFileSystemWatcher:: removePaths (const QStringList & paths )

移除指定 paths 从文件系统看守程序。

返回值是不能成功取消看守的路径的列表。

移除看守失败的原因通常从属系统 (如:可能由于路径已被删除)。

另请参阅 removePath () 和 addPaths ().