How to use QFileSystemWatcher to track folder for change

I am new to QT and I want to use QFileSystemWatcher to monitor the folder. I just can't figure out how to do this.

I am reading http://qt-project.org/doc/qt-4.8/qfilesystemwatcher.html , but I don't know how to initialize it.

I have not found a single example, so now please if someone can post an explanation or a simple example that controls a folder and nothing more.

Oh, and it should run in the console, if that matters.

Thanks for your answers and wishes.

+13
source share
1 answer

Please look at this .h and .cpp, it shows an example ... greetings!

#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(); } 

When you ever edit or create or delete a file or folder in the path "C: / QtTest", you will get a message box.

+26
source

Source: https://habr.com/ru/post/981059/


All Articles