作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何像在 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/
如何像在 QDirModel 中那样使用 QDir::DirsFirst 对 QFileSystemModel 进行排序?QFileSystemModel 没有setSorting 方法。 最佳答案
我是一名优秀的程序员,十分优秀!