Indeed, when you click on the filename column, the sorting will be by filename. if you also need to sort by file type (directory or file), then you need to multi-sort by file name and file type (two columns).
To do this, hold down the Shift key and click on the second column. However, there is one problem in the standard Qt model - QFileSystemModel, a string for the file type is used, which contains a description by file extension. Below is the code that fixes this, just add these changes to Grid_FileBrowser.exe example, i have attachned the full project here -
:
class MyFileSystemModel : public QFileSystemModel
{
public:
enum Roles {
FileTypeRole = QFileSystemModel::FilePermissions + 1,
};
MyFileSystemModel(QObject* parent = nullptr) : QFileSystemModel(parent) {}
QVariant data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.model() != this)
return QVariant();
if (role == FileTypeRole && index.column() == 2)
{
QString s = QFileSystemModel::data(index, Qt::DisplayRole).toString();
if (s != "File Folder")
s = "File";
return s;
}
return QFileSystemModel::data(index, role);
}
};
Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Qtitan::TreeGrid File Browser"))
{
...............
QFileSystemModel* fileSystem = new MyFileSystemModel();
fileSystem->setRootPath(QLatin1String("C:/"));
view->setModel(fileSystem);
....................
column = static_cast<Qtitan::GridBandedTableColumn*>(view->getColumn(2));
column->setBandIndex(detailsBand->index());
column->dataBinding()->setSortRole((Qt::ItemDataRole)MyFileSystemModel::FileTypeRole);
......................}
Or another option, as mentioned above, you can make a Custom Role for the filename column and return a value so that it takes into account the file name and type. In this case clicking on filename will sort by name and type.