0

I have a C++ plugin that watches for file changes with QFileSystemWatcher and connects it's fileChanged signal with a custom QML type slot like this:

//In the custom QML type constructor
QObject::connect(&this->_watcher, SIGNAL(fileChanged(QString)),
                        this, SLOT(fileChangedSlot(QString)));

The slot function:

void CustomQMLTypeClass::fileChangedSlot(QString file)
{
    Q_UNUSED(file);
    emit fileChanged();
}

In the QML side:

CustomQMLType{
    fileUri: "some/file/path/file.format"
    onFileChanged: console.log("File changed")
}

While running the program all goes right, but when I do, i.e.:

echo "sth" >> some/file/path/file.format

More than once, the notification is only triggered once. Why? O.o

Nico Rodsevich
  • 2,393
  • 2
  • 22
  • 32

1 Answers1

0

Apparently the problem is with QFileSystemWatcher, it sometimes worked and some others don't. As I can handle the cost, my quick solution was to alter the slot:

void CustomQMLTypeClass::fileChangedSlot(QString &file)
{
    _watcher.removePath(file);
    _watcher.addPath(file);
    emit fileChanged();
}

Now it works as expected but don't know why and couldn't get to understand neither with QFileSystemWatcher's source. Finally I decided KDE's KDirWatch is way better.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
Nico Rodsevich
  • 2,393
  • 2
  • 22
  • 32