I do what the theme says on a system without a keyboard / mouse, so I need to make this work "from code". When I change the RootIndex QListView, I want to highlight the first row.
Here is mainwindow.cpp from a small test project that I did:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QEvent> #include <QKeyEvent> #include <QDebug> #include <QTimer> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new QFileSystemModel; model->setRootPath("/Users/anders/Downloads/Browser"); listView = new QListView; listView->setModel(model); listView->show(); QTimer::singleShot(2000, this, SLOT(LightItUp1())); } void MainWindow::LightItUp1() { qDebug("LightItUp1"); listView->setRootIndex(model->index("/Users/anders/Downloads")); listView->setCurrentIndex(model->index(0, 0, listView->rootIndex())); QTimer::singleShot(2000, this, SLOT(LightItUp2())); } void MainWindow::LightItUp2() { qDebug("LightItUp2"); listView->setRootIndex(model->index("/Users/anders/Downloads/Browser")); listView->setCurrentIndex(model->index(0, 0, listView->rootIndex())); QTimer::singleShot(2000, this, SLOT(LightItUp3())); } void MainWindow::LightItUp3() { qDebug("LightItUp3"); listView->setRootIndex(model->index("/Users/anders/Downloads")); listView->setCurrentIndex(model->index(0, 0, listView->rootIndex())); QTimer::singleShot(2000, this, SLOT(LightItUp4())); } void MainWindow::LightItUp4() { QString p = "/Users/anders/Downloads/Mail"; listView->setRootIndex(model->index(p)); listView->setCurrentIndex(model->index(0, 0, listView->rootIndex())); } MainWindow::~MainWindow() { delete listView; delete model; delete ui; }
In this example, LightItUp 1-3 does what I want, but LightItUp4 does not. If I change the folders to 2 and 4, then both of them will not be able to do what I want, while 1 and 3 are still working. I suspect that I misunderstood how to use this model / view, but I have no idea what.
Edit: created a simpler example with @buck error checking. See Comments in the source code.
const QString rp = "/home/anders/src/"; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new QFileSystemModel; model->setRootPath(rp); //using model->setRootPath(rp + "/trunk") instead works listView = new QListView; listView->setModel(model); listView->show(); QTimer::singleShot(2000, this, SLOT(LightItUp1())); } void MainWindow::LightItUp1() { qDebug("LightItUp1"); QModelIndex p = model->index(rp + "/trunk"); if (!p.isValid()) { qDebug("index not valid\n"); return; } //model->setRootPath(rp + "/trunk") here does not make it work listView->setRootIndex(p); listView->setCurrentIndex(model->index(0, 0, p)); }
I thought that when I set setRootPath (rp) in the model and then set the view to use the model, the view should move in all rp subfolders if I set the indexes correctly. I will re-read Qtdocs on Model / View, QListView and QFileSystemModel, but would like to publish them in case anyone understands what is going on.