Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileSystemWatcher not updating on file change
Forum Updated to NodeBB v4.3 + New Features

QFileSystemWatcher not updating on file change

Scheduled Pinned Locked Moved Solved General and Desktop
qfilesystemwatcqtcreatorqt 4.8
22 Posts 5 Posters 8.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J.HilkJ J.Hilk

    @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

    Aleksey_KA Offline
    Aleksey_KA Offline
    Aleksey_K
    wrote on last edited by
    #21

    @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 QFileSystemWatcher

    You 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!

    Aleksey_KA 1 Reply Last reply
    0
    • Aleksey_KA Aleksey_K

      @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 QFileSystemWatcher

      You 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!

      Aleksey_KA Offline
      Aleksey_KA Offline
      Aleksey_K
      wrote on last edited by
      #22

      No event loop in my test - that is the problem.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved