That is exactly what I was looking for! Thank you.
Here's my code:
//myitemmodel.h
{
Q_OBJECT
public:
explicit MyItemModel
(QObject *parent
= 0);
explicit MyItemModel
(int rows,
int columns,
QObject* parent
= 0);
virtual bool hasChildren
(const QModelIndex &parent
) const;
};
//myitemmodel.cpp
#include "myitem.h"
MyItemModel
::MyItemModel(QObject *parent
) :{}
MyItemModel
::MyItemModel(int rows,
int columns,
QObject *parent
) :{}
bool MyItemModel
::hasChildren(const QModelIndex &parent
) const {
if(itemFromIndex(parent) != 0)
{
switch(itemFromIndex(parent)->type())
{
case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
/* force children to be hidden, no matter the view type */
return false;
case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
default:
/* proceed normally */
break;
}
}
}
int SessionItemModel
::rowCount(const QModelIndex &parent
) const {
if(itemFromIndex(parent) != 0)
{
switch(itemFromIndex(parent)->type())
{
case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
/* force children to be hidden, no matter the view type */
return 0;
case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
default:
/* proceed normally */
break;
}
}
}
//myitemmodel.h
class MyItemModel : public QStandardItemModel
{
Q_OBJECT
public:
explicit MyItemModel(QObject *parent = 0);
explicit MyItemModel(int rows, int columns, QObject* parent = 0);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual bool hasChildren(const QModelIndex &parent) const;
};
//myitemmodel.cpp
#include "myitem.h"
MyItemModel::MyItemModel(QObject *parent) :
QStandardItemModel(parent)
{}
MyItemModel::MyItemModel(int rows, int columns, QObject *parent) :
QStandardItemModel(rows, columns, parent)
{}
bool MyItemModel::hasChildren(const QModelIndex &parent) const
{
if(itemFromIndex(parent) != 0)
{
switch(itemFromIndex(parent)->type())
{
case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
/* force children to be hidden, no matter the view type */
return false;
case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
default:
/* proceed normally */
break;
}
}
return QStandardItemModel::hasChildren(parent);
}
int SessionItemModel::rowCount(const QModelIndex &parent) const
{
if(itemFromIndex(parent) != 0)
{
switch(itemFromIndex(parent)->type())
{
case MyItem::CUSTOMTYPE1: //QStandardItem::UserType +1
case MyItem::CUSTOMTYPE3: //QStandardItem::UserType +3
case MyItem::CUSTOMTYPE4: //QStandardItem::UserType +4
/* force children to be hidden, no matter the view type */
return 0;
case MyItem::CUSTOMTYPE2: //QStandardItem::UserType +2
default:
/* proceed normally */
break;
}
}
return QStandardItemModel::rowCount(parent);
}
To copy to clipboard, switch view to plain text mode
This had the desired effect of particular item types appearing as if they had no children in my QTreeView. Note that only overriding QStandardItemModel::rowCount() made it so that the expansion button still appeared in the QTreeView even though its children were hidden, so I had to override QStandardItemModel::hasChildren(), too. It seems that overriding rowCount is optional in my case, but I did it anyway to be safe.
Bookmarks