How to display icons in QTableView via a custom QAbstractItemModel?
-
I'm building a custom QAbstractItemModel model.
The first column contains icons, the second one - text.
This is the code of the data method:
@QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
{
if(role != Qt::DisplayRole )
return QVariant();int col = index.column(); if (col == 0) { return iconProvider->icon(QFileIconProvider::Folder); } else if (col == 1) { return "TEXT"; }
}@
But all I get in the resulting Table View is just text in the second column. There's no folder icon in the first column.I know I have to use Qt::DecorationRole, however I never receive it in the role parameter!
-
So silly of me. The solution is obvious:
@QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
{
int col = index.column();if (role== Qt::DecorationRole) { if (col == 0 ) { return iconProvider->icon(QFileIconProvider::Folder); } } else if (role == Qt::DisplayRole) { if (col == 1) { return "HELLO"; } } else { return QVariant(); } }@