Results 1 to 7 of 7

Thread: QFileSystemWatcher hanging in slot

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QFileSystemWatcher hanging in slot

    When connecting a QFileSystemWatcher to a custom slot, the slot is executed once and then hangs. The Application doesn't crash and it is still responsive.

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class CustomWidget : public QObject
    5. { Q_OBJECT
    6. public:
    7. CustomWidget(QWidget* parent=0) : QObject(parent)
    8. { }
    9.  
    10. public slots:
    11. void aSlot()
    12.  
    13. { qDebug() << "aSlot invoked"; }
    14. };
    15.  
    16. #include "main.moc"
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication app(argc, argv);
    21. CustomWidget wid;
    22. watcher.removePath("/test"); //works as expected if commented out
    23. watcher.addPath("/test");
    24. QObject::connect(&watcher, SIGNAL(directoryChanged(QString)) , &wid, SLOT(aSlot() ) );
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    I am using a mac to run the QTCreator application, and this may be the issue.
    I am using an xml file that I am changing while the application runs, I want the extracted values to update some progress bars. It does update once, but then nothing changes after the xml file is changed the second time
    Any time I update a file I want the custom slot to be run. It is run the first time and then hangs.
    I tested this by adding a second variable of type QFileSystemWatcher which is connected to the same custom slot. The behaviour is the same for both files, it updates once but then nothing.
    I assume the problem is with QFileSystemWatcher.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher hanging in slot

    With the code as you posted it - that is, with the slot only plotting a message out - do you get only one message when you run your app?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    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: QFileSystemWatcher hanging in slot

    By "hangs" do you mean "is not called again"?

    What if you monitor the file directly instead of its directory?
    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.


  4. #4
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileSystemWatcher hanging in slot

    I have modified the code to what is below.


    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class CustomWidget : public QObject
    5. { Q_OBJECT
    6. public:
    7. CustomWidget(QWidget* parent=0) : QObject(parent)
    8. { }
    9.  
    10. public slots:
    11. void aSlot()
    12.  
    13. { qDebug() << "aSlot invoked"; }
    14. };
    15.  
    16. #include "main.moc"
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication app(argc, argv);
    21. CustomWidget wid;
    22. watcher.removePath("/test");
    23. watcher.addPath("/test");
    24. QObject::connect(&watcher, SIGNAL(fileChanged(QString)) , &wid, SLOT(aSlot() ) );
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    I changed directoryChanged() to fileChanged(), but still have the same issue where "aSlot invoked" is printed only once, when the slot is called more than once. It's as if the first time the slot is called it never finishes.

    I have just tried this on my PC and "aSlot invoked" is printed every time the xml file is saved. I think this means there is an issue with QFileSystemWatcher on the mac

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QFileSystemWatcher hanging in slot

    Remove any code you have in your slot, except for the qDebug() - does it behave the same?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    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: QFileSystemWatcher hanging in slot

    Quote Originally Posted by jf00071 View Post
    It's as if the first time the slot is called it never finishes.
    If it had never finished then the whole application would have frozen.

    There are some notes regarding Mac in QFileSystemWatcher docs.
    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. #7
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileSystemWatcher hanging in slot

    I have changed it so that just qDebug() is in the slot, but he slot is still called only once.
    I read the docs but i'm using OSX 10.7 and i'm only using one xml file with QFileSystemWatcher, so there should be no issues.

    I have no issues on my friends PC (the slot is called every time the xml file is updated).

Similar Threads

  1. QFIleSystemWatcher
    By jayreddy in forum Qt Programming
    Replies: 1
    Last Post: 15th December 2009, 08:21
  2. QFileSystemWatcher
    By Kumosan in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2008, 20:52
  3. QFileSystemWatcher
    By minty in forum Qt Programming
    Replies: 13
    Last Post: 22nd May 2007, 15:39
  4. Need help with QFileSystemWatcher
    By L.Marvell in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2006, 13:19
  5. Qprocess...hanging entire application!!!!
    By nupul in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2006, 14: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.