title: Keep row selected in QTableView
body: <p>I am working on a program to view and edit records in a file.  It features a <code>QTableView</code> that displays all records, a <code>QLineEdit</code> to search for records, and some labels that display the details of the selected record:</p>
  
  <p><img src="https://i.stack.imgur.com/R5Ols.png" alt="record viewer"> </p>
  
  <p>There is a <code>QAbstractTableModel</code> class that holds the data and a <code>QSortFilterProxyModel</code> class that helps filtering the rows in the <code>QTableView</code>.</p>
  
  <p>Searching and filtering works fine.  Typing text in the search box immediately filters the list of records.  But there are two things I cannot get to work:</p>
  
  <ol>
  <li>if the list is not empty, I want one item to be selected/current always</li>
  <li>the selected/current item must be brought into view when typing in the search box</li>
  </ol>
  
  <p>For example, when I type "tesla", the list will be empty since no item matches.  But as soon as I backspace to "te", the "Forester" matches and I want it to be selected.  Second example: when (after starting the program) I type "f" the list is narrowed down to 8 items and "Pacifica" is selected.  When I erase the "f", the Pacifica is still selected but no longer in the visible part of the list.</p>
  
  <p>I have posted the full source code of this on <a href="http://pastie.org/10910508" rel="nofollow noreferrer">Pastie</a>, and here are some (hopefully) relevant snippets.</p>
  
  <pre><code>void MainWindow::on_lineEditSearch_textChanged(const QString &amp; text)
  {
      itemProxy-&gt;setFilterFixedString(text);
  
      updateStatusBar();
  }
  
  void MainWindow::currentRowChangedSlot(QModelIndex const &amp; current, QModelIndex const &amp; /*previous*/)
  {
      Car * car = 0;
  
      if (current.isValid())
      {
          QModelIndex sibling = current.sibling(current.row(), COLUMN_THIS);
          QVariant variant = itemProxy-&gt;data(sibling);
          car = static_cast&lt;Car *&gt; (variant.value&lt;void *&gt; ());
      }
  
      updateCarMake(car);
      updateCarModel(car);
  }
  
  MainWindow::MainWindow(QWidget * parent, CarItemModel * itemModel, CarSortFilterProxyModel * itemProxy) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      this-&gt;itemModel = itemModel;
      this-&gt;itemProxy = itemProxy;
  
      ui-&gt;setupUi(this);
      setupStatusBar();
  
      ui-&gt;tableView-&gt;setModel(itemProxy);
  
      ui-&gt;tableView-&gt;setColumnHidden(COLUMN_THIS, true);
  
      QItemSelectionModel * selectionModel = ui-&gt;tableView-&gt;selectionModel();
  
      connect(selectionModel, SIGNAL(currentRowChanged(QModelIndex const &amp;, QModelIndex const &amp;)),
              this, SLOT(currentRowChangedSlot(QModelIndex const &amp;, QModelIndex const &amp;)));
  
      connect(selectionModel, SIGNAL(selectionChanged(QItemSelection const &amp;, QItemSelection const &amp;)),
              this, SLOT(selectionChangedSlot(QItemSelection const &amp;, QItemSelection const &amp;)));
  
      ui-&gt;tableView-&gt;selectRow(0);
  
      ui-&gt;lineEditSearch-&gt;setFocus();
  
      updateStatusBar();
  }
  
  &lt;widget class="QTableView" name="tableView"&gt;
   &lt;property name="verticalScrollBarPolicy"&gt;
    &lt;enum&gt;Qt::ScrollBarAlwaysOn&lt;/enum&gt;
   &lt;/property&gt;
   &lt;property name="selectionMode"&gt;
    &lt;enum&gt;QAbstractItemView::SingleSelection&lt;/enum&gt;
   &lt;/property&gt;
   &lt;property name="selectionBehavior"&gt;
    &lt;enum&gt;QAbstractItemView::SelectRows&lt;/enum&gt;
   &lt;/property&gt;
  &lt;/widget&gt;
  </code></pre>
  
  <p>So, my question is: how can I make sure an item is always selected and in the visible part of the list (unless the user is scrolling, of course)?</p>