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 QFileSystemWatcherwrote on 3 Jun 2022, 23:36 last edited by@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!
-
@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!
wrote on 6 Jun 2022, 17:55 last edited byNo event loop in my test - that is the problem.
21/22