QFileSystemWatcher not updating on file change
-
@Aleksey_K said in QFileSystemWatcher not updating on file change:
Any solution for this?
Watch the directory and re-add your watched file. When there is no such a file, then there's nothing to watch.
-
@Christian-Ehrlicher said in QFileSystemWatcher not updating on file change:
@Aleksey_K said in QFileSystemWatcher not updating on file change:
Any solution for this?
Watch the directory and re-add your watched file. When there is no such a file, then there's nothing to watch.
I was wrong: file still in the file watcher after file rewriting - truncated: I've checked
m_fileWatcher.files()
. HoweverQFileSystemWatcher::fileChanged
does not come for some reason. Why? How to understand the reason and fix? Or Qt Bug again? -
Don't know - it may depend on the backend. Mabye look at the bug reporting system or add the workaround I suggested.
-
@Christian-Ehrlicher said in QFileSystemWatcher not updating on file change:
Don't know - it may depend on the backend. Mabye look at the bug reporting system or add the workaround I suggested.
File still in file watcher: if I edit the file in editor - it triggers, if I do:
std::ofstream output_file(file_Name, std::ios_base::out | std::ios_base::trunc); output_file << content; output_file.flush();
does not trigger. Any ideas?
-
@Aleksey_K said in QFileSystemWatcher not updating on file change:
File still in file watcher:
As I said - watch the directory and re-add the file.
-
@Christian-Ehrlicher too complex, need to monitor files list ourselves which is weird. Also file already there in the watcher, so like Qt doc says it won't work:
Adds path to the file system watcher if path exists. The path is not added if it does not exist, or if it is already being monitored by the file system watcher.
-
Then write a bug report or do the watching by yourself with your OS api calls.
-
@Christian-Ehrlicher said in QFileSystemWatcher not updating on file change:
Then write a bug report or do the watching by yourself with your OS api calls.
-
You need to specify which filesystem watcher backend is used and what filesystem you're working on.
Also a minimal, compilable example is needed. -
@Christian-Ehrlicher said in QFileSystemWatcher not updating on file change:
You need to specify which filesystem watcher backend is used and what filesystem you're working on.
Also a minimal, compilable example is needed.etx4. what minimal example? I just connect
QFileSystemWatcher::fileChanged
to a class slot. And it triggers in UI app when I edit a file in an editor. If if do it in tests:void write_data(const std::string &content, const std::string &file_Name) { std::ofstream output_file(file_Name, std::ios_base::out | std::ios_base::trunc); output_file << content; output_file.flush(); }
imitating file change, the signal and therefore my class slot does not trigger for some reason.
-
If you want a solution you should provide the developer a good working base. Creating a simple example and providing essential information to a bug report is therefore a good idea to get your bug fixed. Otherwise it might get closed because it's not reproducible for the developer. But it's up to you - just don't blame us when the bug is closed with 'Needs more info'.
-
@Aleksey_K
here,you're welcome.
#include <QApplication> #include <ios> #include <fstream> #include <QFileSystemWatcher> #include <QPushButton> #include <QFile> #include <QDebug> void write_data(const std::string &content, const std::string &file_Name) { std::ofstream output_file(file_Name, std::ios_base::out | std::ios_base::trunc); output_file << content; output_file.flush(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile f(QCoreApplication::applicationDirPath() + "/testFile.txt"); qDebug() << QCoreApplication::applicationDirPath() + "/testFile.txt"; Q_ASSERT(f.open(QIODevice::WriteOnly)); f.write("asdasdasdasdas"); f.close(); QFileSystemWatcher watcher({QCoreApplication::applicationDirPath()}); QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [](const QString &path)->void{qDebug() << "File changed"<< path;}); QPushButton btn("Modify File"); QObject::connect(&btn, &QPushButton::clicked, [fileName = QCoreApplication::applicationDirPath() + "/testFile.txt"]()->void{ write_data("blublbub", fileName.toStdString()); }); btn.show(); return a.exec(); }
-
@Christian-Ehrlicher said in QFileSystemWatcher not updating on file change:
If you want a solution you should provide the developer a good working base. Creating a simple example and providing essential information to a bug report is therefore a good idea to get your bug fixed. Otherwise it might get closed because it's not reproducible for the developer. But it's up to you - just don't blame us when the bug is closed with 'Needs more info'.
No problem:
DataClass.h:
#pragma once #include <QDebug> #include <QFileSystemWatcher> class DataClass : public QObject { Q_OBJECT public: DataClass() { connect(&m_fileWatcher, &QFileSystemWatcher::fileChanged, // this, &DataClass::reloadData); } void reloadData(QString fileName) { emit dataLoaded(); m_fileWatcher.addPath(fileName); qDebug() << "Data reloaded!"; } signals: void dataLoaded(); private: QFileSystemWatcher m_fileWatcher; };
BugReproTest.cpp:
#include "DataClass.h" #include <gtest/gtest.h> #include <cstdlib> #include <fstream> #include <iostream> #include <QSignalSpy> #include <QString> void write_data(const std::string &content, const std::string &file_Name) { std::ofstream output_file(file_Name, std::ios_base::out | std::ios_base::trunc); output_file << content; output_file.flush(); } TEST(BugRepro, fileWatcher) { DataClass data; QString fileName = "./test_file"; write_data("1", fileName.toStdString()); QSignalSpy dataLoadedSpy(&data, SIGNAL(dataLoaded())); ASSERT_NO_THROW(data.reloadData(fileName)); ASSERT_EQ(dataLoadedSpy.count(), 1); dataLoadedSpy.pop_front(); write_data("2", fileName.toStdString()); ASSERT_EQ(dataLoadedSpy.count(), 1); dataLoadedSpy.pop_front(); }
-
@J-Hilk said in QFileSystemWatcher not updating on file change:
@Aleksey_K
here,
you're welcome.Thanks a lot! I've also added mine above. Are You able to repro? I'm able to repro with Your example as well! Thanks again!
-
@Aleksey_K said in QFileSystemWatcher not updating on file change:
Are You able to repro
I'm,
I also tested QFile, instead of fstream and that also does not trigger QFileSystemWatcher -
@J-Hilk said in QFileSystemWatcher not updating on file change:
@Aleksey_K said in QFileSystemWatcher not updating on file change:
Are You able to repro
I'm,
I also tested QFile, instead of fstream and that also does not trigger QFileSystemWatcherConfirm, I also mentioned that.
-
@J-Hilk said in QFileSystemWatcher not updating on file change:
@Aleksey_K said in QFileSystemWatcher not updating on file change:
Are You able to repro
I'm,
I also tested QFile, instead of fstream and that also does not trigger QFileSystemWatcherYou created wrong example - need to monitor the file, not dir. I've modified it and it works:
#include <QApplication> #include <fstream> #include <ios> #include <QDebug> #include <QFile> #include <QFileSystemWatcher> #include <QPushButton> void write_data(const std::string &content, const std::string &file_Name) { std::ofstream output_file(file_Name, std::ios_base::out | std::ios_base::trunc); output_file << content; output_file.flush(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); QString fileName = QCoreApplication::applicationDirPath() + "/testFile.txt"; QFile f(fileName); qDebug() << fileName; Q_ASSERT(f.open(QIODevice::WriteOnly)); f.write("asdasdasdasdas"); f.close(); QFileSystemWatcher watcher({ fileName }); QObject::connect( &watcher, &QFileSystemWatcher::fileChanged, [](const QString &path) -> void { qDebug() << "File changed" << path; }); QPushButton btn("Modify File"); QObject::connect(&btn, &QPushButton::clicked, [fileName = QCoreApplication::applicationDirPath() + "/testFile.txt"]() -> void { write_data("blublbub", fileName.toStdString()); }); btn.show(); return a.exec(); }
Mine variant does not!