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. TreeView : How could I show directories first?
Forum Updated to NodeBB v4.3 + New Features

TreeView : How could I show directories first?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 778 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
    MSDQuick
    wrote on last edited by
    #1

    Hi guys :

    I wonder if there is a way to sort "TreeView" to show Directories first , then files?

    Thanks.
    MSD.

    Joel BodenmannJ 1 Reply Last reply
    0
    • M MSDQuick

      Hi guys :

      I wonder if there is a way to sort "TreeView" to show Directories first , then files?

      Thanks.
      MSD.

      Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      I assume that you are using QFileSystemModel?
      As far as I know / can tell it's not possible to just flick a switch. However, you can still get this going by implementing a custom sort-filter model (subclassing QSortFilterProxyModel). You should be able to overwrite QSortFilterProxyModel::lessThan() to implement the behavior your are looking for. You would basically compare the isDir() attribute that you can retrieve from the underlying QFileSystemModel.

      Something like this comes to mind (untested!):

      bool MySortFilterModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
      {
      	QFileSystemModel* model = qobject_cast<QFileSystemModel*>(sourceModel());
      	if (!model) {
      		qFatal("Shit hit the fan");
      		return true;
      	}
      	
      	if (!model->fileInfo(left).isDir() && model->fileInfo(right).isDir()) {
      	    return false;
      	}
      
      	return true;
      }
      

      This is just a - most likely not even compiling - example showing the general idea behind this. You'd have to also take the sorting order into account by checking QSortFilterProxyModel::sortOrder().

      I hope that helps somehow.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      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