Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get root index for QTreeView and go back up one level

Tags:

c++

qt

I use QTreeView and QFileSystemModel to make a very simple file explorer. Now the QTreeView can get confusing with it's cascading structure:

image description

This is added an option to set selected directory as root location temporary:

image description

In in the code, this is the method that will set root node to QTreeView by given path. At this moment I don't know how to check on invalid paths:

void FileView::setRootPath( const QString&str )
{
    // This didn't work
    //model->setRootPath(str);
    //This works
    ui.treeView->setRootIndex(model->index(str));
}

But I also want to be able to revert this oparation and go back up the directory tree. What I think I need is to get some code under following comment line:

void FileView::rootUpOneLevel() {
    QString rootPath;
    // Get the current root index path into the QString
     ...
    // Set the path as new root index, provided it's not out of the original root
    setRootPath(rootPath);
}
like image 296
Tomáš Zato - Reinstate Monica Avatar asked Sep 06 '25 03:09

Tomáš Zato - Reinstate Monica


2 Answers

I think, if you want to reset your view and show the whole directory tree you need to simply make:

ui.treeView->setRootIndex(QModelIndex());

i.e. by providing an invalid model index, which is the root model index.

UPDATE

In order to go one level up you will need to call the same setRootIndex() function, but with the parent model index as an argument:

void FileView::up(const QString &str)
{
    QModelIndex idx = model->index(str);
    ui.treeView->setRootIndex(idx.parent());
}

or

// Goes one level up from the current
void FileView::up()
{
    QModelIndex currentRoot = ui.treeView->rootIndex();
    ui.treeView->setRootIndex(currentRoot.parent());
}
like image 64
vahancho Avatar answered Sep 07 '25 23:09

vahancho


So first I thought I can do it using paths, which worked - except on windows, it wouldn't let you list drives again. This is because there is no parent directory of D:\. But otherwise, this works:

void FileView::parentDirectory() {
    QString path = model->fileInfo(ui.treeView->rootIndex()).absoluteDir().absolutePath();
    setRootPath(path);
}

But there's a much better solution that uses indexes directly, avoiding some string manipulations:

void FileView::rootUpOneLevel() {
    ui.treeView->setRootIndex(ui.treeView->rootIndex().parent());
}

This will not do anything if rootIndex().parent() is invalid - setRootIndex already has the check for invalid implemented. This is the right solution.

Also although the validity is checked for you and you can call and apply .parent() as many times as you want without actual errors, this is the correct way to check whether more parent directories exist:

void FileView::rootUpOneLevel() {
    //First go up
    ui.treeView->setRootIndex(ui.treeView->rootIndex().parent());
    //Now if root node isn't valid that means there's no actual root node we could see and display
    emit parentAvailable(ui.treeView->rootIndex().isValid());
}
like image 24
Tomáš Zato - Reinstate Monica Avatar answered Sep 07 '25 21:09

Tomáš Zato - Reinstate Monica