Qt Reference Documentation

# QFileSystemWatcher Class Reference

The QFileSystemWatcher class provides an interface for monitoring files and directories for modifications. More...

     #include <QFileSystemWatcher>

Inherits: QObject.

Note: All functions in this class are reentrant.

This class was introduced in Qt 4.2.

- List of all members, including inherited members

## Public Functions

| QFileSystemWatcher ( QObject * parent = 0 )
| QFileSystemWatcher ( const QStringList & paths, QObject * parent = 0 )
| ~QFileSystemWatcher ()
void | addPath ( const QString & path )
void | addPaths ( const QStringList & paths )
QStringList | directories () const
QStringList | files () const
void | removePath ( const QString & path )
void | removePaths ( const QStringList & paths )

- 29 public functions inherited from QObject

## Signals

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

- 1 signal inherited from QObject

### Additional Inherited Members

- 1 property inherited from QObject
- 1 public slot inherited from QObject
- 7 static public members inherited from QObject
- 8 protected functions inherited from QObject

## Detailed Description

The QFileSystemWatcher class provides an interface for monitoring files and directories for modifications.

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.

Note: On systems running a Linux kernel without inotify support, file systems that contain watched paths cannot be unmounted.

Note: Windows CE does not support directory monitoring by default as this depends on the file system driver installed.

Note: The act of monitoring files and directories for modifications consumes system resources. This implies there is a limit to the number of files and directories your process can monitor simultaneously. On Mac OS X 10.4 and all BSD variants, for example, an open file descriptor is required for each monitored file. Some system limits the number of open file descriptors to 256 by default. This means that addPath() and addPaths() will fail if your process tries to add more than 256 files or directories to the file system monitor. Also note that your process may have other file descriptors open in addition to the ones for files being monitored, and these other open descriptors also count in the total. Mac OS X 10.5 and up use a different backend and do not suffer from this issue.

See also QFile and QDir.

## Member Function Documentation

### QFileSystemWatcher::QFileSystemWatcher ( QObject * parent = 0 )

Constructs a new file system watcher object with the given parent.

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

Constructs a new file system watcher object with the given parent which monitors the specified paths list.

### QFileSystemWatcher::~QFileSystemWatcher ()

Destroys the file system watcher.

### void QFileSystemWatcher::addPath ( const QString & path )

Adds path to the file system watcher if 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 path specifies a directory, the directoryChanged() signal will be emitted when path is modified or removed from disk; otherwise the fileChanged() signal is emitted when path is modified, renamed or removed.

Note: There is a system dependent limit to the number of files and directories that can be monitored simultaneously. If this limit has been reached, path will not be added to the file system watcher, and a warning message will be printed to stderr.

See also addPaths() and removePath().

### void QFileSystemWatcher::addPaths ( const QStringList & paths )

Adds each path in paths to the file system watcher. Paths are not added if they not exist, or if they are already being monitored by the file system watcher.

If a path specifies a directory, the directoryChanged() signal will be emitted when the path is modified or removed from disk; otherwise the fileChanged() signal is emitted when the path is modified, renamed, or removed.

Note: There is 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 added to the file system watcher, and a warning message will be printed to stderr for each path that could not be added.

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 )`[signal]`

This signal is emitted when the directory at a specified path, is modified (e.g., when a file is added, modified 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.

See also fileChanged().

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

This signal is emitted when the file at the specified path is modified, renamed or removed from disk.

See also directoryChanged().

### QStringList QFileSystemWatcher::files () const

Returns a list of paths to files that are being watched.

See also directories().

### void QFileSystemWatcher::removePath ( const QString & path )

Removes the specified path from the file system watcher.

See also removePaths() and addPath().

### void QFileSystemWatcher::removePaths ( const QStringList & paths )

Removes the specified paths from the file system watcher.

See also removePath() and addPaths().