Nominate our 2022 Qt Champions!

Sort the System Drives in QTreeView using QSortFilterProxyModel

  • I have a QTreeview where I am displaying all system drives using QFileSystemModel. I am looking for much better approach where a QTreeview can display Local Drives in one section, Removable Drive in another and so on. Basically sort the drives.

    Here is the code:
    @pSystemPrimaryModel = new QFileSystemModel(this);
    pSystemPrimaryModel->setFilter( QDir::AllDirs | QDir::NoDotAndDotDot );

    // Sets the model for the view to present.

    // Just Display NAME and hide other Columns
    for(int nCount = 1; nCount < pSystemPrimaryModel->columnCount(); nCount++)

    This basically gives me all the drives as follows:

    • C:
    • New Volume(D:)
    • New Volume(E:)
    • SD_Card(F:)
    • Transcend Drive(G:) @

    Using hideColumn() I have hidden all the header(Except Name) which gets displayed when QTreeView is used i.e. Name, Type, Size etc.

    My Requirement:

    1. What I basically want to achieve is this:
      @+ Local Folder
      +New Volume(D:)
      +New Volume(E:)
    • Removable Drives
      +Transcend Drive(G:)@

    Basically separate the local drives and external ones. I have come across QSortFilterProxyModel which looks like it sorts the drive order. But I have no clue how to use that.

    1. I want to remove the header in QTreeView. Since Name is visible in my output, I want to hide it. It would be helpful if you could guide me :)

Log in to reply