C++ (Qt) Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; } QVariant CFileSystemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } else if(role == Qt::CheckStateRole) { return checkedIndexes.contains(index) ? Qt::Checked : Qt::Unchecked; } 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::CheckStateRole) { if(value == Qt::Checked) { checkedIndexes.insert(index); if(hasChildren(index) == true) { recursiveCheck(index, value); } } else { checkedIndexes.remove(index); if(hasChildren(index) == true) { recursiveCheck(index, value); } } emit dataChanged(index, index); return true; } else 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); } bool CFileSystemModel::recursiveCheck(const QModelIndex &index, const QVariant &value) { if(hasChildren(index)) { int i; int childrenCount = rowCount(index); QModelIndex child; for(i=0; i<childrenCount; i++) { child = QFileSystemModel::index(i, 0, index); setData(child, value, Qt::CheckStateRole); } } }
C++ (Qt)#include "cfilesystemmodel.hh"#include <QDebug> CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent) , m_needFilenameWithExt(false) , m_topFolder(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::CheckStateRole) { return checkedIndexes.contains(index) ? Qt::Checked : Qt::Unchecked; } 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::CheckStateRole) { if(value == Qt::Checked) { if (fileInfo(index).isFile() || m_topFolder == false) { checkedIndexes.insert(index); } if(hasChildren(index) == true && m_topFolder == false) { recursiveCheck(index, value); } } else { if (fileInfo(index).isFile() || m_topFolder == false) { checkedIndexes.remove(index); } if(hasChildren(index) == true && m_topFolder == false) { recursiveCheck(index, value); } } emit dataChanged(index, index); return true; } else 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); } bool CFileSystemModel::recursiveCheck(const QModelIndex &index, const QVariant &value) { m_topFolder = true; if(hasChildren(index)) { int i; int childrenCount = rowCount(index); QModelIndex child; for(i=0; i<childrenCount; i++) { child = QFileSystemModel::index(i, 0, index); setData(child, value, Qt::CheckStateRole); } } m_topFolder = false; }
C++ (Qt)... if (fileInfo(index).isFile() || m_topFolder == false) { if(value == Qt::Checked) checkedIndexes.insert(index); else checkedIndexes.remove(index); }...
C++ (Qt)#include "cfilesystemmodel.hh"#include <QDebug> CFileSystemModel::CFileSystemModel(QObject *parent) : QFileSystemModel(parent) , m_needFilenameWithExt(false) , m_topFolder(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::CheckStateRole) { return checkedIndexes.contains(index) ? Qt::Checked : Qt::Unchecked; } 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::CheckStateRole) { if (fileInfo(index).isFile() || m_topFolder == false) { if(value == Qt::Checked) checkedIndexes.insert(index); else checkedIndexes.remove(index); } if(hasChildren(index) == true && m_topFolder == false) { recursiveCheck(index, value); } emit dataChanged(index, index); return true; } else 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); } bool CFileSystemModel::recursiveCheck(const QModelIndex &index, const QVariant &value) { m_topFolder = true; if(hasChildren(index)) { int i; int childrenCount = rowCount(index); QModelIndex child; for(i=0; i<childrenCount; i++) { child = QFileSystemModel::index(i, 0, index); setData(child, value, Qt::CheckStateRole); } } m_topFolder = false; }