title: How to refresh a QSqlTableModel while preserving the selection?
body: <p>I am using a <code>QSqlTableModel</code> and <code>QTableView</code> to view an SQLite database table.</p>
  
  <p>I would like to have the table auto refresh every second or so (it's not going to be a very large table - a couple of hundred rows). And i can do this - like so:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>QTimer *updateInterval = new QTimer(this);
  updateInterval-&gt;setInterval(1000);
  updateInterval-&gt;start();
  connect(updateInterval, SIGNAL(timeout()),this, SLOT(update_table()));
  
  ...
  
  void MainWindow::update_table()
  {
      model-&gt;select(); //QSqlTableModel*
      sqlTable-&gt;reset(); //QTableView*
  }
  </code></pre>
  
  <p>But this removes any selection I have, so the selections only last for up to a second. This is annoying, as another pane in the GUI depends on what is selected. If nothing is selected, then it resets to an explanation splash page.</p>
  
  <p>I then tried a somewhat hacky approach, which gets the selected row number, resets the table, and then selects that row. But this doesn't work either, as the selected row can move up or down based on additions to the table.</p>
  
  <p>I know other classes have a <code>dataChanged()</code> signal, which would be ideal.</p>
  
  <p>Do any of you know <strong>how I could have the table refresh to reflect changes to the database</strong> (from either command line usage, or other instances of the program) <strong>AND keep the current selection?</strong></p>
  
  <p>I know I could get data from the current selection, and then after the reset search for the same row and then reselect it, but this seems like a counter productive and bad solution to the problem.</p>
  
  <p>EDIT: Current attempt at solution:</p>
  
  <pre><code>void MainWindow::update_table()
  {    
  
      QList&lt;QModelIndex&gt; selection = sqlTable-&gt;selectionModel()-&gt;selection().indexes();
      QList&lt;int&gt; selectedIDs;
      bool somethingSelected = true;
  
      for(QList&lt;QModelIndex&gt;::iterator i = selection.begin(); i != selection.end(); ++i){
          int col = i-&gt;column();
          QVariant data = i-&gt;data(Qt::DisplayRole);
  
      if(col == 0) {
              selectedIDs.append(data.toInt());
          }
      }
  
      if(selectedIDs.empty()) somethingSelected = false;
  
      model-&gt;select();
      sqlTable-&gt;reset();
  
      if(somethingSelected){
          QList&lt;int&gt; selectedRows;
  
          int rows = model-&gt;rowCount(QModelIndex());
          for(int i = 0; i &lt; rows; ++i){
              sqlTable-&gt;selectRow(i);
              if(selectedIDs.contains(sqlTable-&gt;selectionModel()-&gt;selection().indexes().first().data(Qt::DisplayRole).toInt())) selectedRows.append(i);
      }
  
      for(QList&lt;int&gt;::iterator i = selectedRows.begin(); i != selectedRows.end(); ++i){
          sqlTable-&gt;selectRow(*i);
      }
  }
  }
  </code></pre>
  
  <p>Okay so this more or less works now...</p>