Important: Please read the Qt Code of Conduct -

QFileSystemWatcher not updating on file change

  • 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(){
         std::ofstream outfile;
         remove(monitor); // Creates a fresh monitor file every time the program is run"monitor", std::ios_base::app);
         outfile << "1";

    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();
           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.

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


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