QFileSystemWatcher Class
The QFileSystemWatcher class provides an interface for monitoring files and directories for modifications. More…
| Header | #include <QFileSystemWatcher> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QFileSystemWatcher(QObject *parent = nullptr) | |
| QFileSystemWatcher(const QStringList &paths, QObject *parent = nullptr) | |
| ~QFileSystemWatcher() | |
| bool | addPath(const QString &file) |
| QStringList | addPaths(const QStringList &files) |
| bool | removePath(const QString &file) |
| QStringList | removePaths(const QStringList &files) |
| QStringList | files() const |
| QStringList | directories() const |
Signals
| void | fileChanged(const QString &path) |
| void | directoryChanged(const QString &path) |
Detailed Description
QFileSystemWatcher monitors the file system for changes to files and directories by watching a list of specified paths.
Call addPath() to watch a particular file or directory. Multiple paths can be added using the addPaths() function. Existing paths can be removed by using the removePath() and removePaths() functions.
QFileSystemWatcher examines each path added to it. Files that have been added to the QFileSystemWatcher can be accessed using the files() function, and directories using the directories() function.
The fileChanged() signal is emitted when a file has been modified, renamed or removed from disk. Similarly, the directoryChanged() signal is emitted when a directory or its contents is modified or removed. Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.
QFileSystemWatcher watcher;
watcher.addPath("/etc/hosts");
QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [&](const QString &path) {
if (QFileInfo::exists(path))
watcher.addPath(path);
});
The watcher uses inotify through a QSocketNotifier, so it needs a running event loop to emit its signals. Events are merged per read: a burst of changes of one path can produce a single signal.
Differences from Qt
The platform is Linux only, using inotify. If inotify is not available, or the kernel limit of watches is reached for a path, that path is polled by comparing stat() results and directory listings every 200 ms instead, which cannot see changes that are undone within an interval. The polled engine can be forced for all paths by giving the watcher the object name _qt_autotest_force_engine_poller before adding paths, as in Qt's tests. Paths are watched as written: two spellings of the same file are two watches. There is no signal parameter QPrivateSignal.
The class is not built for Zephyr, where QT_NO_FILESYSTEMWATCHER is defined instead. See Nt on Zephyr.
Member Function Documentation
QFileSystemWatcher::QFileSystemWatcher(QObject *parent = nullptr)
Constructs a new file system watcher object with the given parent.
See also addPath() and addPaths().
QFileSystemWatcher::QFileSystemWatcher(const QStringList &paths, QObject *parent = nullptr)
Constructs a new file system watcher object with the given parent and starts watching the specified paths.
See also addPaths().
QFileSystemWatcher::~QFileSystemWatcher()
Destroys the file system watcher.
bool QFileSystemWatcher::addPath(const QString &file)
Adds file to the file system watcher if the path exists. The path is not added if it does not exist, or if it is already being monitored by the file system watcher.
If file specifies a directory, the directoryChanged() signal will be emitted when file is modified or removed from disk; otherwise the fileChanged() signal is emitted when file is modified, renamed or removed.
Returns true if the path was added; otherwise returns false. The paths that could not be added can be had by using addPaths().
See also addPaths(), removePath(), files() and directories().
QStringList QFileSystemWatcher::addPaths(const QStringList &files)
Adds each path in files to the file system watcher. Paths are not added if they do not exist, or if they are already being monitored by the file system watcher.
The return value is a list of paths that could not be watched.
See also addPath() and removePaths().
QStringList QFileSystemWatcher::directories() const
Returns a list of paths to directories that are being watched.
See also files().
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, deleted or renamed in it) 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.
A removed directory is no longer watched.
See also fileChanged() and addPath().
void QFileSystemWatcher::fileChanged(const QString &path)
This signal is emitted when the file at the specified path is modified, renamed or removed from disk.
A removed or renamed file is no longer watched: it is dropped from files(), and has to be added again with addPath() if it is created again.
See also directoryChanged() and addPath().
QStringList QFileSystemWatcher::files() const
Returns a list of paths to files that are being watched.
See also directories().
bool QFileSystemWatcher::removePath(const QString &file)
Removes the specified file from the file system watcher.
Returns true if the path was being watched and has been removed; otherwise returns false.
See also removePaths() and addPath().
QStringList QFileSystemWatcher::removePaths(const QStringList &files)
Removes the specified files from the file system watcher.
The return value is a list of paths that were not being watched.
See also removePath() and addPaths().