Results 1 to 10 of 10

Thread: Getting last modified file from QFileSystemWatcher

  1. #1
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Getting last modified file from QFileSystemWatcher

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    I am not sure this can work correctly, you are connecting the same slot to two different signals with two different arguments.

    In the case of directoryChanged the string is the path of the directory, in case of fileChanged it is the path of the file.

    I guess it works in your case because you are not watching any files, only a directory.

    As for the slot being called multiple times:
    is the file the only file being modified? is the file atomically changed, e.g. renamed or truncated?

    As for recursively monitoring: I think QFileSystemWatcher only monitors the directories you tell it to monitor, not its subdirectories.
    You'll have to add them to the watched set to do this.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    is the file the only file being modified? is the file atomically changed, e.g. renamed or truncated?
    I'm modifying a text file named file1 in the specified directory just by adding some text to the file. And after clicking save, immediately this slot is getting called but the output is printed 4 times on the console.

    What I want to achieve is to create a class with a function that gives me the last activity in the specified directory. And I need to update the last activity into a log file.
    Last activity = New file created (or) Existing file modified/deleted (or) new directory created.

    Any hints to implement this ?

    Thank you.
    Last edited by rawfool; 17th March 2014 at 10:46.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    The saving in the text editor might not be just one file change, e.g. the file system could modify multiple blocks in a sequence of operations and notify you about each individual change.

    You could start a timer when you get the change notification and log the modification when it runs out, etc.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    Sorry for this, but I still didn't understand why it is generating the same signal 4 time upon modification of a file once.


    You could start a timer when you get the change notification and log the modification when it runs out, etc
    So if I start a timer on change notification (directoryChanged/fileChanged), what is the mark for me to stop the timer ? And you mean to print to console or enter the details in the log only after stopping the timer?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    Quote Originally Posted by rawfool View Post
    Sorry for this, but I still didn't understand why it is generating the same signal 4 time upon modification of a file once.
    Well, the underlying system, whatever that is in your platform, triggers more than once. Maybe once for the file being opened for write, once for data being added, once for file writing being complete and once for file entry in directory being updated.

    The operation on the file (writing) can always trigger multiple changes, it can be an ongoing operation until the file is being closed.
    There are only very few atomic file manipulations, e.g. rename.

    Quote Originally Posted by rawfool View Post
    So if I start a timer on change notification (directoryChanged/fileChanged), what is the mark for me to stop the timer ?
    The timer would not be stopped, it would timeout, telling you that there had been no further change since it had been started.

    Quote Originally Posted by rawfool View Post
    And you mean to print to console or enter the details in the log only after stopping the timer?
    That is obviously up to you program.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    rawfool (18th March 2014)

  8. #7
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    Thanks for the replies anda_skoa.
    I'm looking for something like inotify equivalent in Qt. Though I'm trying to get things from QFileSystemWatcher, but it's getting cumbersome.

    Thank you.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting last modified file from QFileSystemWatcher

    QFileSystemWatcher is using inotify under the hood.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Getting last modified file from QFileSystemWatcher

    Is there any way to filter results for IN_CREATE | IN_DELETE | IN_MODIFY modes only? As anda_skoa suggested to use timer to suppress multiple signals that gets generated, I did it. But now the results are not accurate.
    Qt Code:
    1. #include "cfilesystemwatcher.h"
    2. #include <QDebug>
    3. #include <QDir>
    4. #include <QFileInfoList>
    5. #include <QDateTime>
    6. #include <QTimer>
    7.  
    8. CFileSystemWatcher::CFileSystemWatcher(QObject *parent) :
    9. {
    10. timer = new QTimer;
    11. timer->setInterval(500);
    12.  
    13. // connections
    14. connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(stateChanged(QString)));
    15. connect(timer, SIGNAL(timeout()), this, SLOT(changedFileName()));
    16. }
    17.  
    18. void CFileSystemWatcher::stateChanged(QString str) // slot
    19. {
    20. if(!timer->isActive())
    21. {
    22. timer->start();
    23. QDir dir;
    24. dir.setPath(str);
    25. QFileInfoList list = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs, QDir::Time);
    26. fileName = list.first().baseName();
    27. }
    28. }
    29.  
    30. QString CFileSystemWatcher::changedFileName(void) // slot
    31. {
    32. timer->stop();
    33.  
    34. if(QString("untitled folder").mid(0, 8) == fileName.toLower().mid(0, 8) ||
    35. QString("new file").mid(0, 8) == fileName.toLower().mid(0, 8) || fileName.isEmpty())
    36. return QString();
    37.  
    38. qDebug() << fileName;
    39. return fileName;
    40. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting last modified file from QFileSystemWatcher

    I don't think there is. You can only use inotify directly instead of going through the Qt wrapper. Remember that Qt implements the least common denominator of what platforms have to offer.

    BTW. Your implementation is not correct. The timer should be restarted each time the slot is fired. It should also be a single shot timer. And certainly it should have a much smaller interval (c.a. 10-50ms, depending on how inotify reports the events).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    rawfool (18th March 2014)

Similar Threads

  1. Replies: 7
    Last Post: 3rd February 2014, 14:27
  2. Replies: 2
    Last Post: 16th July 2013, 07:16
  3. Check file is modified?
    By mamyte03@gmail.com in forum Qt Programming
    Replies: 6
    Last Post: 20th September 2007, 13:03
  4. Check if a file has been modified
    By Dark_Tower in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 21:27
  5. How to link modified file
    By awalesminfo in forum Newbie
    Replies: 4
    Last Post: 22nd March 2006, 17:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.