Use case: Two programs share the same configuration file, and when a program makes a change, it needs another program to respond in a timely manner.
Before actually guessing the sorta, it is estimated that there is a timer, and then periodically check whether the configuration file changes.
Today, the interest came, just a look, sure enough, at the same time also regrets the profound Qt.
Don't talk nonsense, let's look at the internal mechanism:
-----------------------------------------------------
QT provides the Qfilesystemwatcher class to monitor changes to files and directories.
Addpath/addpaths:
Set up the files and directories you want to monitor
Removepath/removepaths:
Remove directories that do not need to be monitored.
Signals:
When a monitored file is modified or deleted, a filechanged () signal is generated.
If the directory being monitored is changed or deleted, a directorychanged () signal is generated.
Q_signals:
void filechanged (const QString &path);
void directorychanged (const QString &path);
Source:
Timer:
void Qpollingfilesystemwatcherengine::run ()
{
Qtimer timer;
Connect (&timer, SIGNAL (), SLOT (timeout ()));
Timer.start (PollingInterval);
(void) exec ();
}
File comparison, basically depends on the file attributes have not changed:
Class FileInfo
{
UINT ownerID;
UINT GroupId;
QFile::P ermissions Permissions;
Qdatetime lastmodified;
Qstringlist entries;
Public
FileInfo (const qfileinfo &fileinfo)
: ownerID (Fileinfo.ownerid ()),
GroupId (Fileinfo.groupid ()),
Permissions (Fileinfo.permissions ()),
LastModified (Fileinfo.lastmodified ())
{
if (Fileinfo.isdir ()) {
Entries = Fileinfo.absolutedir (). Entrylist (qdir::allentries);
}
}
FileInfo &operator= (const qfileinfo &fileinfo)
{
*this = FileInfo (FileInfo);
return *this;
}
BOOL Operator!= (const qfileinfo &fileinfo) const
{
if (Fileinfo.isdir () && Entries! = Fileinfo.absolutedir (). Entrylist (Qdir::allentries))
return true;
Return (ownerID! = Fileinfo.ownerid ()
|| GroupId! = Fileinfo.groupid ()
|| Permissions! = Fileinfo.permissions ()
|| LastModified! = fileinfo.lastmodified ());
}
};
Http://www.xuebuyuan.com/363908.html
Use Qfilesystemwatcher to monitor file and directory changes (internally or using a timer)