QT detect directory and subfolders changes

1.7k views Asked by At

i want to detect any changes in a directory uncluding subfolders :

exemple : if i put a listner in the directory D:\dropbox i want my application shows this :

  • D:\dropbox\folder1\file1.txt is deleted
  • D:\dropbox\folder1\folder2 is created
  • D:\dropbox\public is modified
  • ...

i have found this exemple in the internet but it show only a simple message : "the directory is changed "

please can any body help me ?

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QWidget>
#include <QMessageBox>

class MyClass : public QWidget
{
    Q_OBJECT

public:
    MyClass(QWidget* parent=0)
        :QWidget(parent){}

    ~MyClass(){}

public slots:
    void showModified(const QString& str)
    {
        Q_UNUSED(str)
        QMessageBox::information(this,"Directory Modified", "Your Directory is modified");
    }
};

#endif // MYCLASS_H



#include <QApplication>
#include <QFileSystemWatcher>
#include <QDebug>

#include "MyClass.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    QFileSystemWatcher watcher;
    watcher.addPath("C:/QtTest");

    QStringList directoryList = watcher.directories();
    Q_FOREACH(QString directory, directoryList)
            qDebug() << "Directory name" << directory <<"\n";

    MyClass* mc = new MyClass;

    QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), mc, SLOT(showModified(QString)));

    return app.exec();
}
1

There are 1 answers

0
László Papp On

i have found this exemple in the internet but it show only a simple message : "the directory is changed "

Correct, Qt and QFileSystemWatcher cannot do more for you as of now. You can read more about the plans to extend the support but it is not yet there:

http://blog.rburchell.com/2012/03/qt-51-aka-when-qfilesystemwatcher-might.html

You would need to implement this yourself for each platform, but if you really are into that, you could talk to Robin to collaborate.

Here is part of the blog post to see what was being planned:

In the longer run, I plan to call fileChanged and directoryChanged deprecated. They'll still be emitted (of course) to keep existing code working, but in addition, you'll have (subject to review):

pathCreated(path) - emitted when something is created inside a directory you are monitoring, or if something that didn't exist that you were monitoring for is created (more on this later)

pathDeleted(path) - emitted when something is deleted inside a directory you were monitoring, or something that you were monitoring was deleted

fileModified(path) - emitted when a file you were monitoring is modified (attributes or contents)

I also have early plans to introduce a pathMoved(oldLocation, newLocation), but that one has a lot of caveats: it might only work on certain platforms, in certain phases of the moon, and only if you're very lucky - on many platforms, it will likely continue to be synthesised as a pathDeleted(oldPath) and pathCreated(newPath) (if you're watching the new location).

I also have vague ideas about introducing more syntactically friendly API over the top of this, something like:

QPathMonitor pathMonitor(myPath);

connect(&pathMonitor, SIGNAL(deleted()), SLOT(watchedPathDeleted()));

... etc ...

but that's a bit farther away in that I haven't really thought it through, yet.