PDA

View Full Version : Selecting file in QFileSystemModel tree view



Cougar
2nd January 2011, 16:26
Hello,

I have a tree view displaying a QFileSystemModel, and I need to highlight a specific file in the tree. But since QFileSystemModel is loading the children asynchronously, using selectionModel()->setCurrentIndex() seems to be failing, unless the path has been previously expanded in the tree by the user.

Can anyone recommend a way of selecting a file in QFileSystemModel tree view? The only way I can think of is to manually parse the path, call expand() on the tree nodes, wait until QFileSystemModel fetches the directory (how?) and after the entire path is expanded, call selectionModel()->setCurrentIndex(). Isn't there an easier way?

Thanks!

Cougar
3rd January 2011, 09:59
So I came up with something like this. I'm not too excited about the code, but I couldn't think of a better way.



// assumes that path and rootPath use the same dir separator and the same letter case
void PopulatePathInModel(const QString& path, const QString& rootPath, QFileSystemModel* model)
{
QStringList parts = path.split(QDir::separator());

QString currPath;
foreach (QString part, parts)
{
if (!currPath.isEmpty()) currPath += QDir::separator();
currPath += part;

// no need to populate dirs outside of our area of interest
if (!currPath.startsWith(rootPath)) continue;

if (QFileInfo(currPath).isDir())
{
QModelIndex idx = model->index(currPath);
if (idx.isValid())
{
while (model->canFetchMore(idx))
{
model->fetchMore(idx);
}
}
}
}
}

lxman
7th August 2011, 21:21
I know the thread is a bit dated, but I stumbled across this at http://stackoverflow.com/questions/5242161/how-to-select-a-file-in-qtreeview-for-a-qfilesystemmodel-by-path

Much simpler, methinks.


tree->setCurrentIndex(fsModel->index(QDir::currentPath())); // or any path

christophe
28th August 2011, 02:52
Did you found a better solution yet? I'm having the same problem and doing this do not work:


tree->setCurrentIndex(fsModel->index(myPath));



So I came up with something like this. I'm not too excited about the code, but I couldn't think of a better way.



// assumes that path and rootPath use the same dir separator and the same letter case
void PopulatePathInModel(const QString& path, const QString& rootPath, QFileSystemModel* model)
{
QStringList parts = path.split(QDir::separator());

QString currPath;
foreach (QString part, parts)
{
if (!currPath.isEmpty()) currPath += QDir::separator();
currPath += part;

// no need to populate dirs outside of our area of interest
if (!currPath.startsWith(rootPath)) continue;

if (QFileInfo(currPath).isDir())
{
QModelIndex idx = model->index(currPath);
if (idx.isValid())
{
while (model->canFetchMore(idx))
{
model->fetchMore(idx);
}
}
}
}
}


Added after 19 minutes:

I managed to do it in a more cleaner way but still I'm wondering if there is not a better way to do it:



while (myFileSystemModel->canFetchMore(itemIndex)) {
myFileSystemModel->fetchMore(itemIndex);
itemIndex = itemIndex.parent();
}


We do not have a cleaner way to sync for a QFileSystemModel index?