Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. get complete path of the file selected in QTreeView

get complete path of the file selected in QTreeView

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 4.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by AmrCoder
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Isn't what QFileSystemModel::filePath does ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • AmrCoderA Offline
        AmrCoderA Offline
        AmrCoder
        wrote on last edited by
        #3

        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

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why make your model a global variable ?

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • AmrCoderA AmrCoder

            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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved