Hi everybody
I am trying to create a little treeview based on a QFileSystem. The folders will be selected to perform several analysis with the files inside.
I would like to provide more information in the treeview columns such as the number of files in the subfolder, some values extracted from path name etc.
I spent several hours on google, this forum and others Qt resources and I still have difficulties to understand how it works.
I found different solutions. The one I investigated is to create a subclass of the QFileSystemModel class and overwriting flags, data, setdata methods.
I succeed to did it with this very basic code:
: QFileSystemModel(parent)
{
this->setReadOnly(true);
this
->setFilter
(QDir::Dirs|
QDir::NoDotAndDotDot);
this->setRootPath(path);
headerList = headers;
}
TreeModel::TreeModel(const QString &path, const QStringList &headers, const MainAnalysis &data, QObject *parent)
: QFileSystemModel(parent)
{
this->setReadOnly(true);
this->setFilter(QDir::Dirs| QDir::NoDotAndDotDot);
this->setRootPath(path);
headerList = headers;
}
To copy to clipboard, switch view to plain text mode
int TreeModel
::columnCount(const QModelIndex
& parent
= QModelIndex()) const {
int sup;
sup = headerList.size()-QFileSystemModel::columnCount();
return QFileSystemModel::columnCount();//+sup;
}
int TreeModel::columnCount(const QModelIndex& parent = QModelIndex()) const
{
int sup;
sup = headerList.size()-QFileSystemModel::columnCount();
return QFileSystemModel::columnCount();//+sup;
}
To copy to clipboard, switch view to plain text mode
QVariant TreeModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
for(uint i = 0; i<headerList.size(); i++){
if (section == i && orientation == Qt::Horizontal && role == Qt::DisplayRole)
return headerList.at(i);
}
return QFileSystemModel::headerData(section, orientation, role);
}
QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
for(uint i = 0; i<headerList.size(); i++){
if (section == i && orientation == Qt::Horizontal && role == Qt::DisplayRole)
return headerList.at(i);
}
return QFileSystemModel::headerData(section, orientation, role);
}
To copy to clipboard, switch view to plain text mode
QVariant TreeModel
::data(const QModelIndex
& index,
int role
) const {
qDebug() << "index: " << index << "role: " << role;
if(!index.isValid()){
return QFileSystemModel::data(index,role);}
if(index.column()==0){
if (role == Qt::CheckStateRole) return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;
}
if(index.column()== 2)
{
switch(role)
{
case(Qt::DisplayRole):
case(Qt::TextAlignmentRole):
{return Qt::AlignHCenter;}
default:{}
}
}
return QFileSystemModel::data(index,role);
}
QVariant TreeModel::data(const QModelIndex& index,int role) const
{
QString test;
qDebug() << "index: " << index << "role: " << role;
if(!index.isValid()){
return QFileSystemModel::data(index,role);}
if(index.column()==0){
if (role == Qt::CheckStateRole) return checklist.contains(index) ? Qt::Checked : Qt::Unchecked;
}
if(index.column()== 2)
{
switch(role)
{
case(Qt::DisplayRole):
{return QString("Your text");}
case(Qt::TextAlignmentRole):
{return Qt::AlignHCenter;}
default:{}
}
}
return QFileSystemModel::data(index,role);
}
To copy to clipboard, switch view to plain text mode
Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const {
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
}
Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const {
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
}
To copy to clipboard, switch view to plain text mode
bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::CheckStateRole) {
if (value == Qt::Checked) checklist.insert(index);
else checklist.remove(index);
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::CheckStateRole) {
if (value == Qt::Checked) checklist.insert(index);
else checklist.remove(index);
emit dataChanged(index, index);
return true;
}
return QFileSystemModel::setData(index, value, role);
}
To copy to clipboard, switch view to plain text mode
Parts of these codes are extracted from different wokrs found on forums... may be here.
I understood how to create a column (done in headerData and columnCount...
I understood -- partially-- how to add a checkbox (done in data) and handle if it is checked or not in setData
But I don't understand how to modify item and index values... Is there a clever person to explain how to do that with in a newbie language ?
With these functions, How I can add in the column 2 (for example) the number of subdirectories of the item ?
How I can add the results of the operation on the item path name (a double) ?
Thanks a lot
Bookmarks