如题,重写QFileSystemModel的具体代码如下:
- //FileSystemModel.cpp
- FileSystemModel::FileSystemModel(QObject *parent)
-     : QFileSystemModel(parent)
- {
-     this->setRootPath(QString(""));
-     this->setFilter(QDir::Drives | QDir::AllDirs | QDir::NoDotAndDotDot |QDir::Dirs|QDir::Hidden|QDir::System);
- }
- int FileSystemModel::columnCount(const QModelIndex &parent) const
- {
-     Q_UNUSED(parent);
-     return 1;   
- }
- QVariant FileSystemModel::data(const QModelIndex &index, int role) const
- {
-     if(index.isValid())
-     {
-         if(role == Qt::DecorationRole)  
-         {
-             return QVariant();
-         }
-         else if(role == Qt::DisplayRole)
-         {
-             if(this->fileInfo(index).isRoot())
-             {
-                 return QDir::toNativeSeparators(QString("%1").arg(this->fileInfo(index).absolutePath()));
-             }
-         }
-     }
-     return QFileSystemModel::data(index, role);
- }
- void FileSystemModel::sort(int column, Qt::SortOrder order)
- {
-     //此处如何排序,从C:\到Z:\由上而下显示在QTreeView上
-     emit layoutAboutToBeChanged();
-     const int nodeCount = this->rowCount();
-     for(int i = 0; i < nodeCount; ++i)
-     {
-          for(int j = i+1; j < nodeCount; ++j)
-          {
-             char s1 = this->index(i, column).data().toString().at(0).unicode();
-             char s2 = this->index(j, column).data().toString().at(0).unicode();
-         
-          }
-     }
-     QModelIndexList newList;
-     const int numOldNodes = this->rowCount();
-     newList.reserve(numOldNodes);
-     emit layoutChanged();
- }
- //main.cpp
- FileSystemModel *model = new FileSystemModel;
- QTreeView *treeView = new QTreeView(this);
- treeView->setModel(model);
- treeView->show();
排序后,QTreeView上
显示如下:
C:\
D:\
E:\
...
Z:\
从上到下,由C:\开始递增,若磁盘不存在,则不显示,以上如何重写sort能实现FileSystemModel的排序呢?各位大佬,不知道你们有做过吗?欢迎大佬们指点一二,小弟感激不尽!!