I'm getting last modified file in a directory using a QFileSystemWatcher with QDir & QFileInfoList. I create a slot for directoryChanged(QString) signal in my class inherited from QFileSystemWatcher. So when ever a file is created/modified, the slot stateChanged(QString str) is getting emitted 4 times. I'm unable to understand the reason why the slot is being called 4 times.
Here's my implementation -
Qt Code:
  1. // cfilesystemwatcher.cpp
  2. #include "cfilesystemwatcher.h"
  3. #include <QDebug>
  4. #include <QDir>
  5. #include <QFileInfoList>
  6. #include <QDateTime>
  7.  
  8. CFileSystemWatcher::CFileSystemWatcher(QObject *parent) :
  9. {
  10. // connections
  11. connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(stateChanged(QString)));
  12. connect(this, SIGNAL(fileChanged(QString)), this, SLOT(stateChanged(QString)));
  13. }
  14.  
  15. void CFileSystemWatcher::stateChanged(QString str) // <<--- slot
  16. {
  17. QDir dir;
  18. dir.setPath(str);
  19. QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files, QDir::Time);
  20.  
  21. // Here I'm getting the last modified file.
  22. qDebug() << list.first().baseName(); // <<--- This debug is printing the name 4 times to the console for 1 modification
  23. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. // main.cpp
  2. #include <QCoreApplication>
  3. #include "cfilesystemwatcher.h"
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8.  
  9. CFileSystemWatcher fileWatcher;
  10. fileWatcher.addPath("/home/Rahul/Desktop/");
  11.  
  12. return a.exec();
  13. }
To copy to clipboard, switch view to plain text mode 
Now why is the slot with my qDebug() statement getting called 4 times ?
Also this is not giving the files modified inside the subs directories of the specified path. How do I make it emit signal for changes specified to a directory recursively ?

Thank you.