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 7.3k 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 3 Jun 2022, 05:17 last edited by
    #11

    Then write a bug report or do the watching by yourself with your OS api calls.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    A 1 Reply Last reply 3 Jun 2022, 05:31
    0
    • C Christian Ehrlicher
      3 Jun 2022, 05:17

      Then write a bug report or do the watching by yourself with your OS api calls.

      A Offline
      A Offline
      Aleksey_K
      wrote on 3 Jun 2022, 05:31 last edited by
      #12

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

      https://bugreports.qt.io/browse/QTBUG-103994

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 3 Jun 2022, 05:34 last edited by Christian Ehrlicher 6 Mar 2022, 05:35
        #13

        You need to specify which filesystem watcher backend is used and what filesystem you're working on.
        Also a minimal, compilable example is needed.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        A 1 Reply Last reply 3 Jun 2022, 05:45
        0
        • C Christian Ehrlicher
          3 Jun 2022, 05:34

          You need to specify which filesystem watcher backend is used and what filesystem you're working on.
          Also a minimal, compilable example is needed.

          A Offline
          A Offline
          Aleksey_K
          wrote on 3 Jun 2022, 05:45 last edited by
          #14

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

          J 1 Reply Last reply 3 Jun 2022, 06:00
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 3 Jun 2022, 05:53 last edited by
            #15

            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'.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply 3 Jun 2022, 06:24
            0
            • A Aleksey_K
              3 Jun 2022, 05:45

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

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 3 Jun 2022, 06:00 last edited by
              #16

              @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();
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              A 1 Reply Last reply 3 Jun 2022, 06:25
              0
              • C Christian Ehrlicher
                3 Jun 2022, 05:53

                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'.

                A Offline
                A Offline
                Aleksey_K
                wrote on 3 Jun 2022, 06:24 last edited by
                #17

                @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();
                }
                
                1 Reply Last reply
                0
                • J J.Hilk
                  3 Jun 2022, 06:00

                  @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();
                  }
                  
                  A Offline
                  A Offline
                  Aleksey_K
                  wrote on 3 Jun 2022, 06:25 last edited by Aleksey_K 6 Mar 2022, 06:31
                  #18

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

                  J 1 Reply Last reply 3 Jun 2022, 06:35
                  0
                  • A Aleksey_K
                    3 Jun 2022, 06:25

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

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 3 Jun 2022, 06:35 last edited by
                    #19

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    A 2 Replies Last reply 3 Jun 2022, 06:38
                    0
                    • 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, 06:38 last edited by
                      #20

                      @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

                      Confirm, I also mentioned that.

                      1 Reply Last reply
                      0
                      • 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

                          20/22

                          3 Jun 2022, 06:38

                          • Login

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