0

我有一个 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

不止一次,通知只触发一次。为什么?哦

4

1 回答 1

0

显然问题在于QFileSystemWatcher,它有时有效,而另一些则无效。由于我可以处理成本,我的快速解决方案是更改插槽:

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

现在它按预期工作,但不知道为什么,也无法理解QFileSystemWatcher's source。最后我认为 KDEKDirWatch更好。

于 2016-01-29T22:26:09.120 回答