TOMATO_QT
30th July 2013, 00:02
I have a simple image browser app with a QTreeView backed by a QFileSystemModel instance. The user is able select a directory using a QFileDialog. I have it working but I would like to automatically select the first item in the new directory.
I've searched around and tried many bits of code without success. I am able to set the QTreeView selection through a button press (as well of course as clicking on the QTreeView).
My code is below. The console output from the qDebug statement:
idx row 0 0
first row 0 0
idx is the selected directory index, and I don't know how to tell the treeView to select the first item within the directory.
Am I doing something obviously wrong? Any help appreciated as I've been stuck on this a while.
Setup: OSX 10.7.5 - Qt 5.1 - desktop app.
void DirBrowser::loadDirectory(const QString& dirPath)
{
imageDirectoryPath = dirPath;
saveSettings();
QModelIndex idx = dirmodel->setRootPath(imageDirectoryPath);
myTreeView->setRootIndex(idx);
myTreeView->setCurrentIndex(idx);
QModelIndex first = myTreeView->currentIndex();
qDebug() << "idx" << "row" << idx.row() << idx.column();
qDebug() << "first" << "row" << first.row() << first.column();
myTreeView->selectionModel()->select(first,
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
I've searched around and tried many bits of code without success. I am able to set the QTreeView selection through a button press (as well of course as clicking on the QTreeView).
My code is below. The console output from the qDebug statement:
idx row 0 0
first row 0 0
idx is the selected directory index, and I don't know how to tell the treeView to select the first item within the directory.
Am I doing something obviously wrong? Any help appreciated as I've been stuck on this a while.
Setup: OSX 10.7.5 - Qt 5.1 - desktop app.
void DirBrowser::loadDirectory(const QString& dirPath)
{
imageDirectoryPath = dirPath;
saveSettings();
QModelIndex idx = dirmodel->setRootPath(imageDirectoryPath);
myTreeView->setRootIndex(idx);
myTreeView->setCurrentIndex(idx);
QModelIndex first = myTreeView->currentIndex();
qDebug() << "idx" << "row" << idx.row() << idx.column();
qDebug() << "first" << "row" << first.row() << first.column();
myTreeView->selectionModel()->select(first,
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}