4

I have a simple QTreeView with a QFileSystemModel pointing to the root directory:

#include "mainwindow.h"
#include <QApplication>
#include <QFileSystemModel>
#include <QtGui/QApplication>
#include <QtGui>
#include <QTreeView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFileSystemModel *model = new QFileSystemModel;
    QString dir("/");
    model->setRootPath(dir);
    QTreeView *tree = new QTreeView();
    tree->setModel(model);
    tree->setRootIndex(model->index((dir)));
    tree->show();
    return a.exec();
}

It displays something like this: enter image description here

The item I have selected above is /usr/lib/clang. How can I get the absolute path of the currently selected item?

sj755
  • 3,944
  • 14
  • 59
  • 79

2 Answers2

5

Use view->selectionModel()->selectedIndexes() to obtain selected indexes and fileSystemModel->filePath() to get path for these indexes.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • QTreeView::selectedIndexes is protected, so the only way to call it is to write a class that derives from QTreeView. – Pietro Oct 01 '13 at 10:49
  • +Pavel, works fine. `QModelIndexList list = ui.treeViewCalib->selectionModel()->selectedIndexes();` – TimZaman Feb 12 '14 at 15:05
0
on_tree_clicked(const QModelIndex &index)
{
    model->filePath(index)
}
Vahit
  • 1