PDA

View Full Version : QFIleSystemModel with custom delegate have no access to Qt::DecorationRole?



Scope
17th May 2016, 09:19
Hi,

I have QFileSystemModel and Custom delegate which inherits from QAbstractItemDelegate and inside paint function I try to paint item icon like that


// ICON
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);

but it does nothing, ic.name() return empty string.
Without setting delegate I see "standard" icons provides by QFileSystemModel so problem is in my delegate implementation.

anda_skoa
17th May 2016, 16:26
What do you need the QIcon for?

Have you checked that data() returns a non-empty variant?
And if yes, does it contain a QPixmap?

Cheers,
_

Scope
17th May 2016, 17:33
I added these lines inside delegate paint function


QVariant var = index.data(Qt::DecorationRole);
qDebug()<< "var is null?: " << var.isNull(); // false
qDebug()<< "var is valid?: " << var.isValid(); // true
qDebug()<< "icon is null: " << QIcon(qvariant_cast<QPixmap>(var)).isNull(); // true

but in listView each item has icon...
When in paint function I add line


QIcon ic = QIcon(":/Icons/light/files.png");

then all works great so I think that problem is in file system model which not contain properly icon under Qt::DecorationRole, it is possible?

Scope
17th May 2016, 21:39
Ok I found error


QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));

should be


QIcon ic = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));

mistake was bad cast.

anda_skoa
17th May 2016, 22:39
mistake was bad cast.

Make one wonder why I didn't suggest to check if the variant actually contained a QPixmap.
Oh wait, I did.

Cheers,
_

Scope
17th May 2016, 23:09
Yes, you were right.
I solve that problem thanks to your suggestions.