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. Show root path in QTreeview for QFileSystemModel.
Forum Updated to NodeBB v4.3 + New Features

Show root path in QTreeview for QFileSystemModel.

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 6.1k 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.
  • M Offline
    M Offline
    Mammamia
    wrote on last edited by
    #1

    I have a folder structure like this.

    • Qt_Apps
      • Demo1
      • Demo2

    Now I want to show the contents of the above given directory in the same way as its shown above. Not only the contents but also the parent/root directory name.

    QString path = "D:/Qt_Apps"
    QFileSystemModel* fileModel = new QFileSystemModel(this);
    fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
    //set root path
    fileModel->setRootPath(path);
    
    //create view
    QTreeView* view = new QTreeView(this);
    view->setModel(model);
    

    If I run the above example code then I could see that only the contents of the given directory is displayed as given below

    • Demo1
    • Demo2

    Here the parent directory is omitted.
    Is there any way to show also the parent directory in the treeview structure??

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

      Hi
      I wonder if you use
      http://doc.qt.io/qt-5/qabstractitemview.html#setRootIndex
      with QFileSystemModels methods to get a QModelIndex,
      http://doc.qt.io/qt-5/qfilesystemmodel.html#index-1
      If you can change what is Root.
      Never tried it so might be stupid suggestion :)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mammamia
        wrote on last edited by Mammamia
        #3

        @mrjj

        I have tried that also. But didn't help.

        QString path = "D:/Qt_Apps";
        QModelIndex indx = model->index(path);
        qDebug() << indx.data();   // QVariant(QString, "Qt_Apps")
        
        ...
        view->setRootIndex(indx);
        ...
        
        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok, was just a thought.
          Im not aware of other method to show the root
          when its not a drive.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mammamia
            wrote on last edited by
            #5

            I have repeated my test to verify how setRootIndex() works.

            Suppose if we are currently in directory "D:/Qt_Apps"

            Test-1
            Below code will show the contents of directory "D:/Qt_Apps/ColorApp"

            QModelIndex indx = model->index("D:/Qt_Apps/ColorApp");
            view->setRootIndex(indx);
            

            Test-2
            Now we are in the directory "D:/Qt_Apps/ColorApp"
            Below code will take us back to its parent directory "D:/Qt_Apps"

            QModelIndex indx = model->index("D:/Qt_Apps");
            view->setRootIndex(indx);
            
            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              It's not super starightforward. Reimplementing the model to is an option (and probably the most efficient one) . if you want something out of the box you can use KSelectionProxyModel

              QString path = "D:/Qt_Apps"
              QFileSystemModel* fileModel = new QFileSystemModel(this);
              fileModel->setFilter(QDir::NoDotAndDotDot | QDir::Files);
              //set root path
              fileModel->setRootPath("D:/");
              
              QItemSelectionModel* fileSelection=new QItemSelectionModel(fileModel,this);
              
              KSelectionProxyModel* subFolderProxy=new KSelectionProxyModel(fileSelection,this);
              subFolderProxy->setFilterBehavior(KSelectionProxyModel::SubTrees);
              
              fileSelection->select(fileModel->index("D:/Qt_Apps"),QItemSelectionModel::ClearAndSelect); //this is how you select subfolders
              
              //create view
              QTreeView* view = new QTreeView(this);
              view->setModel(subFolderProxy);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • M Offline
                M Offline
                Mammamia
                wrote on last edited by
                #7

                @VRonin thanks for the Suggestion.. I am planning to re implement the model by myself.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mammamia
                  wrote on last edited by
                  #8

                  I have written my own model by subclassing QStandardItemModel.
                  I have written a blog post with the code and explanation which can be found here,
                  Custom FileSystemModel display

                  mrjjM 1 Reply Last reply
                  1
                  • M Mammamia

                    I have written my own model by subclassing QStandardItemModel.
                    I have written a blog post with the code and explanation which can be found here,
                    Custom FileSystemModel display

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Mammamia
                    Thank you for sharing!

                    1 Reply Last reply
                    0

                    • Login

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