Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: check if a file in folder is changed

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

like image 524
Lwyrn Avatar asked Apr 05 '14 16:04

Lwyrn


People also ask

What is the use of filesize () function in Qt?

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

What is qfileinfo in Qt?

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.

How to return the group of a file in Qt?

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

How to check if a file exists in qfile?

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.


2 Answers

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&)));
...
like image 70
lpapp Avatar answered Oct 19 '22 07:10

lpapp


You're looking for QFileSystemWatcher.

like image 35
Geier Avatar answered Oct 19 '22 09:10

Geier