在QDirModel中如何使用QDir::DirsFirst对QFileSystemModel进行排序?QFileSystemModel没有setSorting方法。
发布于 2012-05-29 04:54:52
据我所知,您不能(在Qt4中)。
默认的排序顺序(按"name“列),或按大小排序的行为类似于QDir::DirsFirst (如果按相反的顺序排序,则为DirsLast ),但按时间或类型排序并不会将目录与普通文件区别对待。
QFileSystemModel没有公开用于更改排序顺序的API,我也看不到有任何机会在QFileSystemModel代码中影响它。
(我在当前的Qt5文档中没有看到任何东西表明这一点已经改变,但这些并不是最终的,我也没有仔细研究过。)
发布于 2013-12-21 13:04:49
也许会有人需要这个。正如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);
}https://stackoverflow.com/questions/10789284
复制相似问题