标题:QFileSystemModel重写后如何实现排序
作者:lwei24
日期:2023-05-23 15:57
内容:
如题,重写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();
& ..