Qt World Summit: Submit your Presentation

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

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

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

    like that.....

    my codes are here


    CTestDlg::CTestDlg(QWidget *parent) :
    ui(new Ui::Dialog)
    //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);



    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...

  • 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.

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

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

  • 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?

  • "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.

  • 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.

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

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

    and Thanks to all

Log in to reply