Results 1 to 8 of 8

Thread: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatcher)

  1. #1
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Exclamation How to know the Name of a file, when a file is copied to a folder(QFileSystemWatcher)

    How to know the Name of a file, when a file is added(copied) to a folder(QFileSystemWatcher)?

  2. #2
    Join Date
    Sep 2013
    Posts
    14
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    When you copied a file to directory.
    connect(watcher,SIGNAL(directoryChanged(QString)), this,SLOT(dirChangedSlot(QString))); will called & you can get file name from QString variable

    where wathcer will be QFileSystemWatcher object.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    I don't think so, according to the documentation, you will get a path of the changed directory, as the object can watch multiple directories at once.
    void QFileSystemWatcher::directoryChanged ( const QString & path ) [signal]
    This signal is emitted when the directory at a specified path, is modified (...)
    You can keep track of the directory content yourself:
    Qt Code:
    1. class MyClass ... {
    2. private slots:
    3. void directoryChanged(const QString& path);
    4. private:
    5. QStringList _currentEntrylist;
    6. };
    7.  
    8. ...
    9.  
    10. MClass::MyClass(...)
    11. {
    12. connect(&_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(directoryChanged(QString)) );
    13. }
    14.  
    15. void MyClass::startMonitor(const QString& path){
    16. _watcher.addPath(path);
    17. const QDir dir(path);
    18. _currentEntryList = dir.entryList();
    19. }
    20.  
    21. void MyClass::directoryChanged(const QString& path){
    22. const QDir dir(path);
    23. const QStringList files = dir.entryList();
    24. // now in "files" you have current content of directory
    25. // and in variable "_currentEntryList" you have previous content of directory
    26. // so in order to know which files were added or removed you can compare the two lists
    27. ... <code to find the difference between two string lists>
    28. //
    29. _currentEntryList = files;
    30. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    @stampede: I would improve the performance of this by storing _currentEntryList as a QSet<QString> rather than a QStringList. QSet<> has very fast lookup, so the job of finding which files are new is to simply iterate over the results of dir.entryList(), calling QSet::contains() for each one.

    You could also convert the new directory list into a QSet<QString> using QSet::fromList( const QStringList & ) and use the various QSet operators to find the differences between the old and new sets - in other words, files that have been added, files that have been removed, etc. Saves writing a lot of code to iterate over lists and compare them.

    Probably reduces the number of bugs in the program too.

    Qt Code:
    1. // For example
    2.  
    3. void MyClass::directoryChanged( const QString & path )
    4. {
    5. const QDir dir(path);
    6. QSet<QString> newDirSet = QSet<QString>::fromList( dir.entryList() );
    7.  
    8. // Files that haven't changed
    9. QSet<QString> sameFiles = _currentEntrySet & newDirSet ;
    10.  
    11. // Files that have been added
    12. QSet<QString> newFiles = newDirSet - _currentEntrySet;
    13.  
    14. // Files that have been removed
    15. QSet<QString> deletedFiles = _currentEntrySet - newDirSet ;
    16.  
    17. // and update the current set
    18. _currentEntrySet.swap( newDirSet );
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 1st February 2014 at 22:39.

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    @d_stranz: you are right. It was just an idea that came straight out of my mind into the forum post, looks like it is more natural for me to think of a "list" of files rather than a "set"

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    @stampede: Well, if dir.entryList() returns the list of files in alphabetical order, that's one advantage over using the QSet if you want to display the files to the user. QSet stores entries in whatever order the internal hashing determines, so iterating over the contents of the QSet will not return an alphabetical list, even if they were inserted in alphabetical order. If you wanted to display the current contents in alphabetical order, you'd have to copy the QSet into a QStringList, then sort the list. This isn't so bad, since QString instances with the same values are shared.

    So let's use the best of both our ideas - keep the current directory contents as a list, but use sets to determine what has changed:

    Qt Code:
    1. void MyClass::directoryChanged( const QString & path )
    2. {
    3. const QDir dir(path);
    4. QStringList newEntryList = dir.entryList();
    5. QSet<QString> newDirSet = QSet<QString>::fromList( newEntryList );
    6. QSet<QString> currentDirSet = QSet<QString>::fromList( _currentEntryList );
    7.  
    8. // Files that haven't changed
    9. QSet<QString> sameFiles = currentDirSet & newDirSet;
    10.  
    11. // Files that have been added
    12. QSet<QString> newFiles = newDirSet - currentDirSet;
    13.  
    14. // Files that have been removed
    15. QSet<QString> deletedFiles = currentDirSet - newDirSet;
    16.  
    17. // and update the current set
    18. _currentEntryList = newEntryList;
    19. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2013
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    Thank you ...
    I implemented the same logic , but the problem is- In some cases, although entryList() gives file name, but the file takes time to exist physically . i.e- when I tried to load image(through QImage),while copy of file already in process, then most of time i got blank / partial images.
    How can I solve this problem.(QFile().exist() failed whether file present completely or not).
    I am thinking about SIGNAL SLOTS..
    Need helpful suggestions.Thank you

    Quote Originally Posted by d_stranz View Post
    @stampede: Well, if dir.entryList() returns the list of files in alphabetical order, that's one advantage over using the QSet if you want to display the files to the user. QSet stores entries in whatever order the internal hashing determines, so iterating over the contents of the QSet will not return an alphabetical list, even if they were inserted in alphabetical order. If you wanted to display the current contents in alphabetical order, you'd have to copy the QSet into a QStringList, then sort the list. This isn't so bad, since QString instances with the same values are shared.

    So let's use the best of both our ideas - keep the current directory contents as a list, but use sets to determine what has changed:

    Qt Code:
    1. void MyClass::directoryChanged( const QString & path )
    2. {
    3. const QDir dir(path);
    4. QStringList newEntryList = dir.entryList();
    5. QSet<QString> newDirSet = QSet<QString>::fromList( newEntryList );
    6. QSet<QString> currentDirSet = QSet<QString>::fromList( _currentEntryList );
    7.  
    8. // Files that haven't changed
    9. QSet<QString> sameFiles = currentDirSet & newDirSet;
    10.  
    11. // Files that have been added
    12. QSet<QString> newFiles = newDirSet - currentDirSet;
    13.  
    14. // Files that have been removed
    15. QSet<QString> deletedFiles = currentDirSet - newDirSet;
    16.  
    17. // and update the current set
    18. _currentEntryList = newEntryList;
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: How to know the Name of a file, when a file is copied to a folder(QFileSystemWatc

    Quote Originally Posted by blue_sky View Post
    Thank you ...
    I implemented the same logic , but the problem is- In some cases, although entryList() gives file name, but the file takes time to exist physically . i.e- when I tried to load image(through QImage),while copy of file already in process, then most of time i got blank / partial images.
    How can I solve this problem.(QFile().exist() failed whether file present completely or not).
    I am thinking about SIGNAL SLOTS..
    Need helpful suggestions.Thank you
    Question seems to have moved to this thread http://www.qtcentre.org/threads/5791...-exists-or-not

Similar Threads

  1. QFileSystemWatcher empty QStringList file()
    By Carmengg in forum Newbie
    Replies: 4
    Last Post: 30th January 2014, 11:57
  2. Replies: 0
    Last Post: 23rd December 2013, 13:31
  3. Replies: 2
    Last Post: 28th October 2011, 08:46
  4. Replies: 4
    Last Post: 3rd August 2010, 08:17
  5. Replies: 3
    Last Post: 21st August 2009, 12:39

Tags for this Thread

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.