c – Qt QFileSystemWatcher:信号fileChanged()仅发出一次
发布时间:2020-12-16 03:45:48 所属栏目:百科 来源:网络整理
导读:我正在尝试QFileSystemWatcher,它不知何故不能按预期工作.或者我做错了什么? 我已将QFileSystemWatcher设置为观看单个文件.当我第一次修改文件时,fileChanged()会被激活,这没关系.但是当我再次修改文件时,fileChanged()不再被激活. 这是源代码: main.cpp中
我正在尝试QFileSystemWatcher,它不知何故不能按预期工作.或者我做错了什么?
我已将QFileSystemWatcher设置为观看单个文件.当我第一次修改文件时,fileChanged()会被激活,这没关系.但是当我再次修改文件时,fileChanged()不再被激活. 这是源代码: main.cpp中 #include <QApplication> #include "mainwindow.h" int main(int argc,char **argv) { QApplication app(argc,argv); MainWindow window; window.show(); return app.exec(); } mainwindow.h #include <QDebug> #include <QFileSystemWatcher> #include <QMainWindow> #include <QString> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(); private slots: void directoryChanged(const QString & path); void fileChanged(const QString & path); private: QFileSystemWatcher * watcher; }; mainwindow.cpp #include "mainwindow.h" MainWindow::MainWindow() { watcher = new QFileSystemWatcher(this); connect(watcher,SIGNAL(fileChanged(const QString &)),this,SLOT(fileChanged(const QString &))); connect(watcher,SIGNAL(directoryChanged(const QString &)),SLOT(directoryChanged(const QString &))); watcher->addPath("path to directory"); watcher->addPath("path to file"); } void MainWindow::directoryChanged(const QString & path) { qDebug() << path; } void MainWindow::fileChanged(const QString & path) { qDebug() << path; } 谢谢您的回答. 编辑1 我在Linux下运行此代码. 编辑2 我实际上需要检查某个目录给出的树中的所有MetaPost文件,无论它们是否被修改.我可能会坚持我的替代解决方案,即每秒运行QTimer并手动检查所有文件. QFileSystemWatcher可能在内部以类似的方式执行此操作,但可能更有效. 解决方法
刚才有同样的问题.好像QFileSystemWatcher认为即使文件被修改也会被删除.至少在Linux文件系统上.我的简单解决方案是:
watcher->addPath(path); 将上面的内容添加到fileChanged()的处理程序中.根据需要更改单词观察者. [编辑]删除了QFile :: exists(),用addPath()处理. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |