来自 C++ 插件的 QML 类型仅发出一次信号

QML type from C++ Plugin signaling only once

我有一个 C++ 插件,它使用 QFileSystemWatcher 监视文件更改,并将它的 fileChanged 信号与这样的自定义 QML 类型插槽连接起来:

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

槽函数:

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

QML端:

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

虽然 运行 程序一切正常,但当我这样做时,即:

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

不止一次,通知只触发一次。为什么? O.o

显然问题出在 QFileSystemWatcher,它有时有效,而有些则无效。 由于我能承受成本,我的快速解决方案是更改插槽:

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

现在它按预期工作,但不知道为什么,也无法理解 QFileSystemWatcher's source. Finally I decided KDE's KDirWatch 更好。