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

QFileSystemWatcher not updating on file change

Scheduled Pinned Locked Moved Solved General and Desktop
qfilesystemwatcqtcreatorqt 4.8
22 Posts 5 Posters 7.2k Views
  • 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 J.Hilk
    3 Jun 2022, 06:35

    @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

    A Offline
    A Offline
    Aleksey_K
    wrote on 3 Jun 2022, 23:36 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!

    A 1 Reply Last reply 6 Jun 2022, 17:55
    0
    • A Aleksey_K
      3 Jun 2022, 23:36

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

      A Offline
      A Offline
      Aleksey_K
      wrote on 6 Jun 2022, 17:55 last edited by
      #22

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

      1 Reply Last reply
      0

      21/22

      3 Jun 2022, 23:36

      • Login

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