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. [SOLVED] Need to show Only File System folders in QTreeView.
QtWS25 Last Chance

[SOLVED] Need to show Only File System folders in QTreeView.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 9.5k Views
  • 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
    M_31
    wrote on last edited by
    #1

    Hi All,
    I want to display only folders in my QTreeView with the following order like

    @
    ->Desktop
    |->MY Computer
    |-> C:
    |-> D:
    |->Control Panel

    @
    like that.....

    my codes are here

    @

    CTestDlg::CTestDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    //QFileSystemModel *model = new QFileSystemModel(this);

    QDirModel *model = new QDirModel(this);
    QString home_files = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
    QModelIndex homeIndex = model->index(home_files);
    
    //QString home_path = model->filePath(homeIndex);
    //QModelIndex root_index = model->setRootPath(home_path);
    
    
    ui->treeLocalFolders->setModel(model);
    ui->treeLocalFolders->setRootIndex(homeIndex);
    

    }

    @

    Here i can see all the folders with corresponding files are displaying in my QTreeView
    ...and also the order was not correct...which i am expecting like above
    Please throw some light on this...

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      You can sort ascending or desceding. If you want a custom sort you will have to use a proxy model or reimplement the sort() method.

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

        Thanks Lukas...meanwhile ..how can i show only folders on my QTreeView..instead of folders + files ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cincirin
          wrote on last edited by
          #4

          Did you try to "setFilter":http://doc.qt.nokia.com/latest/qfilesystemmodel.html#setFilter on model ?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            It should be:
            @model->setFilter( QDIr::Dirs )@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              M_31
              wrote on last edited by
              #6

              thanks for your reply.
              Yes, i have used setFilter to List My Directory alone.. but i am not able see my "My Computer" item in my QtreeView.

              Please let me know..how can i get "My Computer" item in my QTreeView?

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fluca1978
                wrote on last edited by
                #7

                "My Computer" is a Window-ism, so I think there is no way to get it out from Qt. What you can have is a list of drives (c:, ecc.) with QDir::drives(). Maybe there is also a way to get the desktop folder but I don't know it.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  QDirModel and QFileSystemModel (which should be used as QDirModel is deprecated) operate on files and folders only. Items like "My Computer" or "Control Panel" are neither the one nor the other.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #9

                    Try something like this ...
                    @
                    QFileSystemModel fileSystemModel = new QFileSystemModel(QDir::NoDotAndDotDot | QDir::Dirs);
                    QModelIndex modelIndex = fileSystemModel->setRoorPath(fileSystemModel->myComputer());
                    ui->treeLocalFolders->setRootIndex(modelIndex)
                    @

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      M_31
                      wrote on last edited by
                      #10

                      Thanks cincirin...its working fine with fileSystemModel->myComputer()..

                      and Thanks to all

                      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