title: How to refresh a QFileSystemModel in a QTreeView after files change through another process?
body: <p>I have a <code>QTreeView</code> with a <code>QFileSystemModel</code> as the model.  Files and directories load correctly.</p>
  
  <p>In my application workflow, a different process copies and overwrites files on the file system.</p>
  
  <p>However, my <code>QTreeView</code> does not update the item/row for the overwritten file (eg: the size and lastModified values for the file do not update to the new values).</p>
  
  <p>Using the file path, I'm able to get a <code>FileInfo</code> that DOES have the updated lastModified value.  However, using that <em>same path</em> to grab the <code>QModelIndex</code> of the row's lastModified value results in it returning the old value.</p>
  
  <p>I've tried a few things (see below) to no avail.</p>
  
  <p>PLEASE let me know if you are aware of how to resolve this.  Many Thanks! :)</p>
  
  <pre><code>// ... at this point the file system has been updated and the file
  // has new values for things like last modified date
  QFileInfo *updatedFile = new QFileInfo( filePath );
  
  QModelIndex indexOfFileName = myFileTreeView-&gt;myFileSystemModel-&gt;index( filePath );
  QModelIndex indexOfLastModifiedDate = myFileTreeView-&gt;myFileSystemModel-&gt;index( filePath, lastModifiedColumnIndex );
  
  // attempts to kick the QFileSystemModel and QTreeView to update the model values
  // a collection from the internet :)
  emit myFileTreeView-&gt;dataChanged( indexOfFileName, indexOfLastModifiedDate );
  emit myFileTreeView-&gt;myFileSystemModel-&gt;dataChanged( indexOfFileName, indexOfLastModifiedDate );
  myTreeView-&gt;repaint();
  myTreeView-&gt;update( indexOfLastModifiedDate );
  myTreeView-&gt;viewport()-&gt;update();
  
  // looking to see if values changed
  QModelIndex newIndexOfLastModifiedDate = myTreeView-&gt;myFileSystemModel-&gt;index( filePath, lastModifiedColumnIndex );
  
  // this shows the correct row and column for the index, so the index looks correct
  qDebug() &lt;&lt; "newIndexOfLastModifiedDate: " &lt;&lt; newIndexOfLastModifiedDate;
  
  // does not have new value
  qDebug() &lt;&lt; "newIndexOfLastModifiedDate.data(): " &lt;&lt; newIndexOfLastModifiedDate-&gt;data();
  
  // has new value
  qDebug() &lt;&lt; "fileInfo.lastModified(): " &lt;&lt; updatedFile-&gt;lastModified();
  </code></pre>
  
  <p><strong>[EDIT - adding steps to simulate application workflow]</strong></p>
  
  <p>The following I believe to be the steps that can mimic the issue.</p>
  
  <p><strong>Steps to Reproduce:</strong></p>
  
  <ul>
  <li><p>Setup a simple QTreeView that uses a QFileSystemModel as its model.</p></li>
  <li><p>Set Root Path to a directory called <code>Root</code></p></li>
  <li><p>Create a text file, <code>Test.txt</code> inside of the <code>Root</code> dir</p></li>
  <li><p>Load the application and observe in it the <code>Test.txt</code> file's <em>Last Modified Date</em> in the <code>Root</code> dir. </p></li>
  <li><p><strong>Keep this application window open.</strong></p></li>
  <li><p>Copy <code>Test.txt</code> to a <em>different</em> directory, say <code>Temp</code></p></li>
  <li><p>Modify the file and save in <code>Temp</code></p></li>
  <li><p>Copy and <em>Replace</em> <code>Test.txt</code> to overwrite the file in <code>Root</code> dir</p></li>
  <li><p>Observe the <em>Last Modified Date</em> in the application window</p></li>
  </ul>
  
  <p>Result: The <em>Last Modified Date</em> is not updated</p>
  
  <p><strong>ADDED SAPMLE:</strong></p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QFileSystemModel&gt;
  #include &lt;QFile&gt;
  #include &lt;QFileInfo&gt;
  #include &lt;QTimer&gt;
  #include &lt;QDebug&gt;
  #include &lt;QTreeView&gt;
  #include &lt;QDateTime&gt;
  
  
  // Globals
  QFileSystemModel *model = NULL;
  const QString name = "test.txt";
  const int delayOffset = 1000;
  
  // Interface
  void onDataChanged( const QModelIndex&amp; topLeft, const QModelIndex&amp; bottomRight, const QVector&lt; int &gt;&amp; roles );
  void clean();
  void doCreateFile();
  void doUpdateFile();
  void doTest();
  
  
  // Implementation
  int main( int argc, char *argv[] )
  {
      QApplication a( argc, argv );
      int delay = 0;
  
      // Clean
      clean();
  
      // Init model
      const QString rootPath = QCoreApplication::applicationDirPath();
      model = new QFileSystemModel( &amp;a );
      model-&gt;setRootPath( rootPath );
      QObject::connect( model, &amp;QFileSystemModel::dataChanged, &amp;onDataChanged );
  
      // Init file actions
      delay += delayOffset * 2;
      QTimer tCreate;
      tCreate.setSingleShot( true );
      tCreate.setInterval( delay );
      QObject::connect( &amp;tCreate, &amp;QTimer::timeout, &amp;doCreateFile );
  
      delay += delayOffset * 4;
      QTimer tUpdate;
      tUpdate.setSingleShot( true );
      tUpdate.setInterval( delay );
      QObject::connect( &amp;tUpdate, &amp;QTimer::timeout, &amp;doUpdateFile );
  
      // GUI
      QTreeView w;
      w.setModel( model );
      w.setRootIndex( model-&gt;index( rootPath ) );
      w.show();
      w.expandAll();
  
      qDebug() &lt;&lt; "Start";
      tCreate.start();
      tUpdate.start();
  
      return a.exec();
  }
  
  void onDataChanged( const QModelIndex&amp; topLeft, const QModelIndex&amp; bottomRight, const QVector&lt; int &gt;&amp; roles )
  {
      qDebug() &lt;&lt; "Model changed";
  }
  
  void clean()
  {
      const QString path = QString( "%1/%2" ).arg( QCoreApplication::applicationDirPath() ).arg( name );
      QFile f( path );
  
      if ( f.exists() )
          f.remove();
  }
  
  void doCreateFile()
  {
      const QString path = QString( "%1/%2" ).arg( QCoreApplication::applicationDirPath() ).arg( name );
      QFile f( path );
  
      if ( !f.open( QIODevice::WriteOnly ) )
      {
          qDebug() &lt;&lt; "Could not create file";
          return ;
      }
  
      f.write( "Preved" );
      f.close();
  
      qDebug() &lt;&lt; "File created";
      doTest();
  }
  
  void doUpdateFile()
  {
      const QString path = QString( "%1/%2" ).arg( QCoreApplication::applicationDirPath() ).arg( name );
      QFile f( path );
  
      if ( !f.open( QIODevice::Append ) )
      {
          qDebug() &lt;&lt; "Could not open file for modification";
          return ;
      }
  
      f.write( " medved" );
      f.close();
  
      qDebug() &lt;&lt; "File updated";
      doTest();
  }
  
  void doTest()
  {
      const QString path = QString( "%1/%2" ).arg( QCoreApplication::applicationDirPath() ).arg( name );
      QFileInfo info( path );
  
      qDebug() &lt;&lt; "Last modified (real):  " &lt;&lt; info.lastModified().toString( "hh:mm:ss" );
      qDebug() &lt;&lt; "Last modified (model): " &lt;&lt; model-&gt;lastModified( model-&gt;index( path ) ).toString( "hh:mm:ss" );
  }
  </code></pre>