PDA

View Full Version : Help drawing shadow for a QPixmap with QPainter



jiveaxe
4th July 2013, 12:52
Hi, I need your precious help for drawing shadows in QAbstractItemDelegate::paint() event. This is a summary: my application has a QListView and a QListWidget. I used two different widgets since the former have to contains hundreds of item, each with few controls, while the latter few items, each with more controls (self-hiding buttons, labels, etc) but when in rest state they look very similar. Each item in two lists show a pixmap that I draw in paint() event for the QListView while using a simple QLabel for the QListWidget. Now I added a shadow around the QLabel in QListWidget's item with the following code:


QGraphicsDropShadowEffect *coverShadow = new QGraphicsDropShadowEffect(this);
coverShadow->setBlurRadius(10.0);
coverShadow->setColor(QPalette::Shadow);
coverShadow->setOffset(0.0);
label->setGraphicsEffect(coverShadow);

It works fine.

Now I would apply something similar to the pixmap in QListView's items. I tryed using QLinerGradient and QRadialGradient but the result is not satisfactory. The shadow is different from that in QListWidget.

In docs i found that setGraphicsEffect() (used in the code above) is a method of QWidget and QGraphicsItem. I tryed with a QGraphicsPixmapItem this way:



QGraphicsPixmapItem gpItem(mPixmap);
gpItem.setTransformationMode(Qt::SmoothTransformat ion);
QGraphicsDropShadowEffect *coverShadow = new QGraphicsDropShadowEffect;
coverShadow->setBlurRadius(10.0);
coverShadow->setColor(QPalette::Shadow);
coverShadow->setOffset(0.0);
gpItem.setGraphicsEffect(coverShadow);
painter->drawPixmap(shadowRect, gpItem.pixmap());


The idea was to load the pixmap, apply the effect and return the final result, but probably it doesn't work this way since I obtain an up-scaled portion of the original pix (never used QGraphicsItem).

Have you any idea how to solve this?

Regards