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
QtWS25 Last Chance

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.
  • M Offline
    M Offline
    mister_m
    wrote on 3 Aug 2016, 18:51 last edited by
    #1

    I'm having a little trouble diagnosing this issue. I'm having an external program create a file and continuously append to it 1 digit, therefore the modified date and size both change. These are things FileSystemWatcher should be able to notice.

    External program:

    int main(){
       while(1){
         std::ofstream outfile;
         remove(monitor); // Creates a fresh monitor file every time the program is run
         outfile.open("monitor", std::ios_base::app);
         outfile << "1";
         sleep(500);
     }
    }
    

    This works perfectly fine.

    Now in QT I run this external program using shell which also works.

    void NewWindow::on_pushButton_clicked()
    {
    
        if(started == false){
          /*Create new Extern process */
            QString path =  QCoreApplication::applicationDirPath();
            QString command = "cd " + path + "/program/bin/ && ./program";
            process = new QProcess;
            process->start("sh",QStringList() << "-c" << command);
            bool status = QObject::connect( rx_process, SIGNAL( started() ), this, SLOT( process_started() ) );  
            started = true;
       }
    }
    

    This then goes into process_started() which houses the FileWatcher

    void TetraWindow::process_started(){
    
        /*Monitor DB File */
           QFileSystemWatcher *databaseWatcher = new QFileSystemWatcher(this);
           QString monitor_path = QCoreApplication::applicationDirPath();
           monitor_path.append("program/bin/monitor");
           databaseWatcher->addPath(monitor_path);
           QObject::connect(databaseWatcher, SIGNAL(fileChanged(QString)), this, SLOT(refreshView()));
    }
    

    Finally refreshView() contains a query that pulls all the elements from a database and displays them in a QTableView.

    In my actual program I'm writing out to this monitor file ever time I add a new database entry. (Addendum: I could monitor the DB itself but the file grows in 1024byte chunks and i miss lots of entries, that’s why i'm writing out to a separate file and monitoring that).

    When i call refreshView() via a btn press, my table is queried and updated correctly.

    So whats working:
    External Program that appends monitor file
    RefreshView() which updates my QTableView
    Process which starts and runs the external program

    Every time i push the start button which launches my external program my TableView is updated because the monitor file has been deleted and recreated. So some elements of QFileSystemWatcher is working, it's at least able to know that the file exists.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Paul Colby
      wrote on 3 Aug 2016, 22:39 last edited by
      #2

      Hi @mister_m, and welcome :)

      I'm not certain, but I suspect the issue is that you're watching a file that no longer exists. To explain...

      • when the watcher begins watching program/bin/monitor, the OS (depends on the FS being used) will usually setup a watch on a specific FS ID (such as the starting inode on ExtFS), and watches that - not the "path" itself.
      • when program removes that file, and creates a new one, the new file gets a new ID, and the watcher keeps watching the old ID which no longer changes (even though its removed from the disk, the OS will usually keep this ID around / valid until all dependant applications have closed their references to it).

      Anyway, that's largely just speculation, but I would try one or both of the following:

      1. don't remove the file, but truncate it instead - that will "empty" the file, but keep the FS ID intact; and/or
      2. setup a watcher on the program/bin directory itself, then you should be seeing if the monitor file (or indeed, any file) in that directory is changed, including being added / removed / re-created etc.

      I hope that helps.

      Cheers.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mister_m
        wrote on 4 Aug 2016, 12:01 last edited by
        #3

        Hi Paul, I appreciate the kind reply and thank you.

        Those were good suggestions. I should of been truncating from the beginning instead of recreating the file, this is a better approach. Implementing this did solve the problem with the monitor path directly to my file. Implementing you other suggestion however did not work (Linking watcher to the directory), which I don’t fully understand since I thought it would look for any changes within the directory including the file change.

        Best

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Aleksey_K
          wrote on 3 Jun 2022, 04:51 last edited by
          #4

          Seems truncating is also deletes the file first, so file watcher stops monitoring it. Any solution for this?

          C 1 Reply Last reply 3 Jun 2022, 04:53
          0
          • A Aleksey_K
            3 Jun 2022, 04:51

            Seems truncating is also deletes the file first, so file watcher stops monitoring it. Any solution for this?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 3 Jun 2022, 04:53 last edited by
            #5

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

            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, 04:57
            0
            • C Christian Ehrlicher
              3 Jun 2022, 04:53

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

              A Offline
              A Offline
              Aleksey_K
              wrote on 3 Jun 2022, 04:57 last edited by
              #6

              @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(). However QFileSystemWatcher::fileChanged does not come for some reason. Why? How to understand the reason and fix? Or Qt Bug again?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 3 Jun 2022, 05:01 last edited by
                #7

                Don't know - it may depend on the backend. Mabye look at the bug reporting system or add the workaround I suggested.

                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:05
                0
                • C Christian Ehrlicher
                  3 Jun 2022, 05:01

                  Don't know - it may depend on the backend. Mabye look at the bug reporting system or add the workaround I suggested.

                  A Offline
                  A Offline
                  Aleksey_K
                  wrote on 3 Jun 2022, 05:05 last edited by Aleksey_K 6 Mar 2022, 05:05
                  #8

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

                  C 1 Reply Last reply 3 Jun 2022, 05:10
                  0
                  • A Aleksey_K
                    3 Jun 2022, 05:05

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

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 3 Jun 2022, 05:10 last edited by
                    #9

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

                    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:16
                    0
                    • C Christian Ehrlicher
                      3 Jun 2022, 05:10

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

                      A Offline
                      A Offline
                      Aleksey_K
                      wrote on 3 Jun 2022, 05:16 last edited by Aleksey_K 6 Mar 2022, 05:16
                      #10

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

                      1 Reply Last reply
                      0
                      • 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.HilkJ 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.HilkJ Offline
                                  J.HilkJ 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.HilkJ 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.HilkJ 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.HilkJ Offline
                                        J.HilkJ 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.HilkJ 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

                                          • Login

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