title: Qt: Browsing filesystem with QListView and QFileSystemModel. How to higlight first item in a folder?
body: <p>I'm doing what the topic says on a system without keyboard/mouse, so I need to make this work "from code". When I change the RootIndex of the QListView I want to highlight the first row.</p>
  
  <p>Here's mainwindow.cpp from a small testproject I've made:</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  #include &lt;QEvent&gt;
  #include &lt;QKeyEvent&gt;
  #include &lt;QDebug&gt;
  #include &lt;QTimer&gt;
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      model = new QFileSystemModel;
      model-&gt;setRootPath("/Users/anders/Downloads/Browser");
  
      listView = new QListView;
      listView-&gt;setModel(model);
      listView-&gt;show();
  
      QTimer::singleShot(2000, this, SLOT(LightItUp1()));
  
  }
  
  void MainWindow::LightItUp1()
  {
      qDebug("LightItUp1");
      listView-&gt;setRootIndex(model-&gt;index("/Users/anders/Downloads"));
      listView-&gt;setCurrentIndex(model-&gt;index(0, 0, listView-&gt;rootIndex()));
  
      QTimer::singleShot(2000, this, SLOT(LightItUp2()));
  }
  
  void MainWindow::LightItUp2()
  {
      qDebug("LightItUp2");
      listView-&gt;setRootIndex(model-&gt;index("/Users/anders/Downloads/Browser"));
      listView-&gt;setCurrentIndex(model-&gt;index(0, 0, listView-&gt;rootIndex()));
  
      QTimer::singleShot(2000, this, SLOT(LightItUp3()));
  }
  
  
  void MainWindow::LightItUp3()
  {
      qDebug("LightItUp3");
      listView-&gt;setRootIndex(model-&gt;index("/Users/anders/Downloads"));
      listView-&gt;setCurrentIndex(model-&gt;index(0, 0, listView-&gt;rootIndex()));
  
      QTimer::singleShot(2000, this, SLOT(LightItUp4()));
  }
  
  
  void MainWindow::LightItUp4()
  {
      QString p = "/Users/anders/Downloads/Mail";
      listView-&gt;setRootIndex(model-&gt;index(p));
      listView-&gt;setCurrentIndex(model-&gt;index(0, 0, listView-&gt;rootIndex()));
  }
  
  MainWindow::~MainWindow()
  {
      delete listView;
      delete model;
      delete ui;
  }
  </code></pre>
  
  <p>In this example LightItUp 1-3 do what I want, but LightItUp4 does not. If I swap the folders in 2 &amp; 4 both of them fail to do what I want, while 1 &amp; 3 still work. I suspect I have misunderstood something about how to use this Model/View, but have no idea what.</p>
  
  <p>Edit: created a simpler example with the error checking @buck mentioned. See the comments in the source code.</p>
  
  <pre><code>const QString rp = "/home/anders/src/";
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      model = new QFileSystemModel;
      model-&gt;setRootPath(rp); //using model-&gt;setRootPath(rp + "/trunk") instead works
  
      listView = new QListView;
      listView-&gt;setModel(model);
      listView-&gt;show();
  
      QTimer::singleShot(2000, this, SLOT(LightItUp1()));
  
  }
  
  void MainWindow::LightItUp1()
  {
      qDebug("LightItUp1");
      QModelIndex p = model-&gt;index(rp + "/trunk");
      if (!p.isValid()) {
          qDebug("index not valid\n");
          return;
      }
  
      //model-&gt;setRootPath(rp + "/trunk") here does not make it work
      listView-&gt;setRootIndex(p);
      listView-&gt;setCurrentIndex(model-&gt;index(0, 0, p));
  }
  </code></pre>
  
  <p>I thought that when I do setRootPath(rp) on the model, and then set the view to use the model, the view should able to move around in all subfolders of rp if I set the indexes correctly. I'll reread the Qtdocs on Model/View, QListView and QFileSystemModel, but wanted to post this in case someone understands what is happening.</p>