Use Qfilesystemwatcher to monitor file and directory changes (internally or using a timer)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.