Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get Absolute Path of Currently Selected Item in QTreeView

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?

like image 229
sj755 Avatar asked Oct 22 '25 05:10

sj755


1 Answers

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

like image 88
Pavel Strakhov Avatar answered Oct 23 '25 21:10

Pavel Strakhov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!