gpt4 book ai didi

qt - QFileSystemModel 排序 DirsFirst

转载 作者:行者123 更新时间:2023-12-05 08:59:59 27 4
gpt4 key购买 nike

如何像在 QDirModel 中那样使用 QDir::DirsFirst 对 QFileSystemModel 进行排序?QFileSystemModel 没有setSorting 方法。

最佳答案

也许有人会需要这个。正如 Kuba Ober 在评论中提到的那样,我已经使用 QFileSystemModel 的 QSortFilterProxyModel 实现了目录优先排序。可能还不完美,但仍然是正确的方向。

bool MySortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
// If sorting by file names column
if (sortColumn() == 0) {
QFileSystemModel *fsm = qobject_cast<QFileSystemModel*>(sourceModel());
bool asc = sortOrder() == Qt::AscendingOrder ? true : false;

QFileInfo leftFileInfo = fsm->fileInfo(left);
QFileInfo rightFileInfo = fsm->fileInfo(right);


// If DotAndDot move in the beginning
if (sourceModel()->data(left).toString() == "..")
return asc;
if (sourceModel()->data(right).toString() == "..")
return !asc;

// Move dirs upper
if (!leftFileInfo.isDir() && rightFileInfo.isDir()) {
return !asc;
}
if (leftFileInfo.isDir() && !rightFileInfo.isDir()) {
return asc;
}
}

return QSortFilterProxyModel::lessThan(left, right);
}

关于qt - QFileSystemModel 排序 DirsFirst,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10789284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com