Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

get complete path of the file selected in QTreeView



  • Hello,
    I have a QTreeView which I use it with QFileSystemModel to get all the files in the directory. using this code

        QFileSystemModel *model = new QFileSystemModel;
        model->setRootPath(QDir::currentPath()); //any path
        ui->treeView->setModel(model);
        ui->treeView->setRootIndex(model->index(QDir::currentPath())); // to show only this path
    

    and I created a signal for click on the QTreeView to get the selected file from it

    void MainWindow::on_treeView_clicked(const QModelIndex &index)
    {
        qDebug() << "row = " << index.row() << " - column = " << index.column();
        qDebug() << "Item = " << ui->treeView->model()->data(index).toString();
    }
    

    I got only the name of the file pure without its full path.

    1 - my question here how can i get the full path of the file when the user select an item from the tree.

    2 - also a second question is there any way to filter the shown files in the directories?

    I tried to setFilter to the model but this only filter the full type of the file like the show only files or directory I want to filter it in the way in which I only choose the file extension to show only the mp3 file or mp4 only and so on.
    Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    Isn't what QFileSystemModel::filePath does ?



  • Thank you this worked I made the model global and I can access the file full path using this function in the QTreeView click signal.
    do you know how I can set filter depends on the file extension to be shown?
    I tried something like this on my model

    model->setFilter(QDir::Files);
    

    but I don't want that I want to filter only the file extension every file in his own directory.
    something like this

    model->setFilter("*.mp3 .*mp4");
    

    or similar


  • Lifetime Qt Champion

    Why make your model a global variable ?

    You can use QSortFilterProxyModel for sorting/filtering/etc..



  • @AmrCoder

    model->setFilter("*.mp3 .*mp4");

    For this you want setNameFilters() rather than setFilter().
    See http://doc.qt.io/qt-5/qfilesystemmodel.html#setNameFilters
    and read e.g. https://stackoverflow.com/questions/17166866/filtering-qfilesystemmodel


Log in to reply