Results 1 to 11 of 11

Thread: QFilesystemwatcher in another thread

  1. #1
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default QFilesystemwatcher in another thread

    Hello,

    I'm trying to monitor a file deletion from a certain path.
    While this file is not deleted, my application shouldn't do anything.
    Of course, if something goes wrong and the file is not deleted, there will be a timeout.

    I have a 3 signals depending on what happens with the file, removed, modified or created. When the file is removed, for example, the signal removed set a variable "finished" to break the loop on the file main.cpp, but it looks like it isn't. Any help on this?

    My test code is attached here: teste-wait.zip

    If this is needed, my OS is Windows 7 Professional.

    Thanks,
    Renan

  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: QFilesystemwatcher in another thread

    Your problem is that you are doing a cross-thread signal/slot connection but do not run an event loop in the receiver object's thread (main thread).

    So when the object in the worker thread emits its signals, the event that would call the MainClass' slots cannot be delivered.

    However, the main question is why do you need a thread at all?

    Cheers,
    Kevin

  3. #3
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QFilesystemwatcher in another thread

    I need to know when a particular file has been deleted in order to proceed with my application.
    I need to block the main thread of my application waiting for it to be deleted. There is no need at all to have another thread...
    Do you have any suggestions on how to do it?

    Thanks,
    Renan

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

    Default Re: QFilesystemwatcher in another thread

    You don't need to block your main thread while the file is not deleted. You need to do something when the file is deleted. You don't need any extra threads for that.
    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.


  5. #5
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QFilesystemwatcher in another thread

    We have a application here were I work, that when we write a file ("close.txt") in some specific path it closes itself. But this operation takes some time. It depends on what it was doing... Sometimes it takes more than 200 seconds to do it.
    My application will write this file to close our other application and will wait until it closes. My signal is when the file is deleted.
    The problem is that I need to wait for it to be deleted. I thought of blocking the main thread, but like you said, I don't need to do it. So how should I proceed with this?

    Renan

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

    Default Re: QFilesystemwatcher in another thread

    You just return to the event loop and let the filesystem watcher call your slot you connected to one of its signals. Since your example doesn't run an event loop at all, you need to adjust it for the loop to be running.
    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.


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

    RenanBS (7th May 2013)

  8. #7
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QFilesystemwatcher in another thread

    I did it using the class QEventLoop. Would it be the recommended way to run this event loop?
    This is what I did:

    main.cpp:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QThread>
    3.  
    4. #include <iostream>
    5.  
    6. #include "filewatcher.h"
    7.  
    8. int main (int argc, char *argv[])
    9. {
    10. QCoreApplication a (argc, argv);
    11. QEventLoop loop;
    12. FileWatcher *close = new FileWatcher ("c:/home/rs94036/");
    13.  
    14. QObject::connect (close, SIGNAL (removed ()), close, SLOT (removedSlot ()));
    15. QObject::connect (close, SIGNAL (modified ()), close, SLOT (modifiedSlot ()));
    16. QObject::connect (close, SIGNAL (created ()), close, SLOT (createdSlot ()));
    17. QObject::connect (close, SIGNAL (exitLoop ()), &loop, SLOT (quit ()));
    18. close->closeSepApp ();
    19.  
    20. loop.exec ();
    21.  
    22. std::cout << "AFTER LOOP" << std::endl;
    23.  
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 

    FileWatcher.cpp:
    Qt Code:
    1. #include <QFileInfo>
    2. #include <QTextStream>
    3.  
    4. #include "FileWatcher.h"
    5.  
    6. #include <iostream>
    7.  
    8. FileWatcher::FileWatcher (const QString &path)
    9. {
    10. monitoringPath = path;
    11. status = FILE_TIMEOUT;
    12. }
    13.  
    14. bool FileWatcher::closeSepApp ()
    15. {
    16. monitoringFileName = "close.txt";
    17. QFile *close = new QFile (monitoringPath + monitoringFileName);
    18. if (!close->exists ())
    19. {
    20. if (close->open (QIODevice::WriteOnly))
    21. {
    22. // Write something on it
    23. QTextStream *text = new QTextStream (close);
    24. *text << "1";
    25. close->close ();
    26. }
    27. else
    28. return false;
    29. }
    30. else
    31. return false;
    32.  
    33. startMonitoring ();
    34. return true;
    35. }
    36.  
    37. void FileWatcher::startMonitoring ()
    38. {
    39. existsBeforeEvent = QFileInfo (monitoringPath + monitoringFileName).exists();
    40. status = existsBeforeEvent ? FILE_CHANGED : FILE_TIMEOUT;
    41.  
    42. mon = new QFileSystemWatcher ();
    43. connect (mon, SIGNAL (fileChanged (QString)), this, SLOT (onFileChanged (QString)));
    44. mon->addPath (monitoringPath + monitoringFileName);
    45. }
    46.  
    47. void FileWatcher::onFileChanged (const QString& path)
    48. {
    49. bool existsAfterEvent = QFileInfo (path).exists();
    50.  
    51. if (existsBeforeEvent && !existsAfterEvent)
    52. emit removed ();
    53. else if (!existsBeforeEvent && existsAfterEvent)
    54. emit created ();
    55. else
    56. emit modified ();
    57. }
    58.  
    59. void FileWatcher::removedSlot ()
    60. {
    61. std::cout << "FILE_REMOVED" << std::endl;
    62. emit exitLoop ();
    63. }
    64.  
    65. void FileWatcher::modifiedSlot ()
    66. {
    67. std::cout << "FILE_CHANGED" << std::endl;
    68. emit exitLoop ();
    69. }
    70.  
    71. void FileWatcher::createdSlot ()
    72. {
    73. std::cout << "FILE_CREATED" << std::endl;
    74. emit exitLoop ();
    75. }
    To copy to clipboard, switch view to plain text mode 

    FileWatcher.h:
    Qt Code:
    1. #ifndef FileWatcher_H
    2. #define FileWatcher_H
    3.  
    4. #include <QFileSystemWatcher>
    5. #include <QObject>
    6. #include <QEventLoop>
    7.  
    8. enum FileStatus
    9. {
    10. FILE_CREATED,
    11. FILE_CHANGED,
    12. FILE_REMOVED,
    13. FILE_TIMEOUT
    14. };
    15.  
    16. class FileWatcher : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. FileWatcher (const QString& path);
    21. bool closeSepApp ();
    22.  
    23. private:
    24. QString monitoringPath;
    25. QString monitoringFileName;
    26. bool existsBeforeEvent;
    27. FileStatus status;
    28.  
    29. public slots:
    30. void startMonitoring ();
    31.  
    32. protected slots:
    33. void onFileChanged (const QString& path);
    34.  
    35. signals:
    36. void removed ();
    37. void modified ();
    38. void created ();
    39. void exitLoop ();
    40.  
    41. public slots:
    42. void removedSlot ();
    43. void modifiedSlot ();
    44. void createdSlot ();
    45.  
    46. };
    47.  
    48. #endif // FileWatcher_H
    To copy to clipboard, switch view to plain text mode 

    Renan
    Last edited by RenanBS; 6th May 2013 at 15:19.

  9. #8
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QFilesystemwatcher in another thread

    Since this method is working fine, I'm assuming it is correct.

    Thanks.

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

    Default Re: QFilesystemwatcher in another thread

    You might have just called a.exec() instead.
    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.


  11. #10
    Join Date
    Oct 2010
    Location
    Porto Alegre
    Posts
    19
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QFilesystemwatcher in another thread

    I thought about that.
    In the example I could have done this. But in my application I don't have access to the QCoreApplication a anymore...

    Thanks.

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

    Default Re: QFilesystemwatcher in another thread

    Sure you did -- QCoreApplication::instance() is a static method that returns a pointer to the application object.
    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.


Similar Threads

  1. QFileSystemWatcher problem... yes, again..
    By nateriver in forum Qt Programming
    Replies: 0
    Last Post: 31st December 2009, 16:47
  2. QFIleSystemWatcher
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 08:21
  3. QFileSystemWatcher
    By Kumosan in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2008, 20:52
  4. QFileSystemWatcher
    By minty in forum Qt Programming
    Replies: 13
    Last Post: 22nd May 2007, 15:39
  5. Need help with QFileSystemWatcher
    By L.Marvell in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2006, 13:19

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
  •  
Qt is a trademark of The Qt Company.