【问题标题】:QFileSystemModel sorting DirsFirstQFileSystemModel 排序 DirsFirst
【发布时间】:2013-12-21 13:16:37
【问题描述】:

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

【问题讨论】:

    标签: qt sorting qfilesystemmodel


    【解决方案1】:

    也许有人会需要这个。正如 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);
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,您不能(在 Qt4 中)。

      默认排序顺序(按“名称”列),或按大小排序的行为类似于QDir::DirsFirst(或DirsLast,如果是相反的顺序),但按时间或类型排序不会区别对待目录普通文件。

      QFileSystemModel 没有公开用于更改排序顺序的 API,而且我在QFileSystemModel 代码中看不到任何影响它的机会。

      (我在当前的 Qt5 文档中没有看到任何表明这已经改变的内容,但这些都不是最终的,我没有仔细查看。)

      【讨论】:

      • 谢谢,我最终按尺寸列排序。
      • 典型的做法是添加一个代理模型,按照您想要的方式对其进行排序。
      猜你喜欢
      • 1970-01-01
      • 2012-09-03
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      相关资源
      最近更新 更多