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 () 函數。

The 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 ().

[虛擬] 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 ().