#include "cfilesystemmodel.hh"#include <QDebug> CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent){} Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; } QVariant CFileSystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); }// else if (role == Qt::DisplayRole)// {// return fileInfo(index).baseName();// } else if (role == Qt::EditRole) { return fileInfo(index).baseName(); } else { return QFileSystemModel::data(index, role); } } bool CFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { return QFileSystemModel::setData(index, value.toString() + "." + fileInfo(index).completeSuffix(), role); } return QFileSystemModel::setData(index, value, role); }
C++ (Qt)#include "cfilesystemmodel.hh"#include <QDebug> CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent){} Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; } QVariant CFileSystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } else if (role == Qt::DisplayRole) { return fileInfo(index).fileName(); } else if (role == Qt::EditRole) { return fileInfo(index).baseName(); } else { return QFileSystemModel::data(index, role); } } bool CFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { return QFileSystemModel::setData(index, value.toString() + "." + fileInfo(index).completeSuffix(), Qt::DisplayRole); /// <<< } return QFileSystemModel::setData(index, value, role); }
else if (role == Qt::DisplayRole) { return fileInfo(index).baseName(); }
C++ (Qt)#include "CFileSystemModel.h" CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent){ setRootPath("C:/"); setReadOnly(false);} CFileSystemModel::~CFileSystemModel(){ } Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; } QVariant CFileSystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } else if (role == Qt::DisplayRole) { return fileInfo(index).fileName(); } else if (role == Qt::EditRole) { return fileInfo(index).baseName(); } else { return QFileSystemModel::data(index, role); } } bool CFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { return QFileSystemModel::setData(index, value.toString() + "." + fileInfo(index).completeSuffix(), role); } return QFileSystemModel::setData(index, value, role); } int CFileSystemModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 1; }
C++ (Qt)#include "cfilesystemmodel.hh"#include <QDebug> CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent) , m_needFilenameWithExt(false){} Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; } QVariant CFileSystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } else if (role == Qt::DisplayRole && fileInfo(index).isFile() ) { return m_needFilenameWithExt ? fileInfo(index).fileName() : fileInfo(index).baseName(); } else if (role == Qt::EditRole) { return fileInfo(index).baseName(); } else { return QFileSystemModel::data(index, role); } } bool CFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole && fileInfo(index).isFile()) /// <<< { m_needFilenameWithExt = true; bool result = QFileSystemModel::setData(index, value.toString() + "." + fileInfo(index).completeSuffix(), role); m_needFilenameWithExt = false; return result; } return QFileSystemModel::setData(index, value, role); }
switch (role) { case Qt::EditRole: return fileInfo(index).baseName(); break; case Qt::DisplayRole: { if (fileInfo(index).isFile()) return m_needFilenameWithExt ? fileInfo(index).fileName() :fileInfo(index).baseName(); else return QFileSystemModel::data(index, role); break; } default: return QFileSystemModel::data(index, role); break; }