There any way to trigger an action if a file in a specified directory ( or in a subfolder ) without fetching all modification times every time ? I'm asking because i've to check this live
This function was introduced in Qt 6.0. Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned. If the file is a symlink, the size of the target file is returned (not the symlink). See also exists ().
QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt resource.
Returns symLinkTarget () as a std::filesystem::path. This function was introduced in Qt 6.0. See also symLinkTarget (). Returns the group of the file. On Windows, on systems where files do not have groups, or if an error occurs, an empty string is returned. This function can be time consuming under Unix (in the order of milliseconds).
One is using [static] bool QFile::exists (const QString &fileName), e.g.: You can also use QFileInfo Class to check the existance of file. Sample code. Checks files exists, if not exists, file gets created and data is written to the file.
You need to use the QFileSystemWatcher.
More importantly, this is the signal you need to connect to:
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().
So, you could write something like this in your class or function:
...
QFileSystemWatcher watcher;
watcher.addPath("/My/Path/To/The/File");
QObject::connect(&watcher, SIGNAL(fileChanged(const QString&)), receiver, SLOT(handleFileChanged(const QString&)));
...
You're looking for QFileSystemWatcher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With