Question

I am using QSortFilterProxyModel in QTreeView for exploring all the files and folders of a directory. Its working fine. but my problem is when i click on any of the item of QTreeView. I am not able to get its information like full path. Can anyone please tell me how to get its full path of the file.

Thanks,

Was it helpful?

Solution

You can calculate the full path with the following way. First you need to track mouse clicks on the QTreeView with connecting the clicked() signal to the appropriate slot:

connect(treeview, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onItemClicked(const QModelIndex &)); 

In your slot you can call the helper function that will return the full path of the clicked tree node:

void TreeView::onItemClicked(const QModelIndex &index)
{
    QString path = fullPath(index);
    // ..
}

QString TreeView::fullPath(const QModelIndex &index)
{
    QString path('/');
    QModelIndex parent = index;
    while (parent.isValid()) {
        path.prepend('/' + parent.data().toString());
        parent = parent.parent();
    }
    return path;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top