PDA

View Full Version : QFileSystemWatcher hanging in slot



jf00071
7th March 2012, 15:51
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.


#include <QtGui>
#include <QDebug>

class CustomWidget : public QObject
{ Q_OBJECT
public:
CustomWidget(QWidget* parent=0) : QObject(parent)
{ }

public slots:
void aSlot()

{ qDebug() << "aSlot invoked"; }
};

#include "main.moc"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CustomWidget wid;
QFileSystemWatcher watcher;
watcher.removePath("/test"); //works as expected if commented out
watcher.addPath("/test");
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)) , &wid, SLOT(aSlot() ) );
return app.exec();
}

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.

high_flyer
7th March 2012, 16:03
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?

wysota
7th March 2012, 16:18
By "hangs" do you mean "is not called again"?

What if you monitor the file directly instead of its directory?

jf00071
8th March 2012, 11:53
I have modified the code to what is below.



#include <QtGui>
#include <QDebug>

class CustomWidget : public QObject
{ Q_OBJECT
public:
CustomWidget(QWidget* parent=0) : QObject(parent)
{ }

public slots:
void aSlot()

{ qDebug() << "aSlot invoked"; }
};

#include "main.moc"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CustomWidget wid;
QFileSystemWatcher watcher;
watcher.removePath("/test");
watcher.addPath("/test");
QObject::connect(&watcher, SIGNAL(fileChanged(QString)) , &wid, SLOT(aSlot() ) );
return app.exec();
}

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

high_flyer
8th March 2012, 13:43
Remove any code you have in your slot, except for the qDebug() - does it behave the same?

wysota
8th March 2012, 14:11
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.

jf00071
9th March 2012, 11:40
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).