PDA

View Full Version : Adding QGraphicsDropShadowEffect to part of a "modified" QGraphicsPixmapItem?



dobedidoo
15th January 2010, 15:10
Hi,

In my GUI application I show what i call ModelObjectGraphicsItem's in a QGraphicsView. An example is (hopefully) seen below:
__________________________________________________ _________________
http://www.ludd.ltu.se/~ubbe/TD120i.png
__________________________________________________ _________________
(I try to attach the image file as well...)

However, I'd prefer not to drop shadow on the text labels but only on the "boxes". Any ideas on how I avoid this?

Here's an overview of my ModelObjectGraphicsItem class:

ModelObjectGraphicsItem::ModelObjectGraphicsItem(G raphicsScene* modelScene, ModelObject* modelObject) :
QGraphicsPixmapItem(*modelObject->pixmap(), 0, modelScene)
{
modelObject_ = modelObject;

QPoint& pos = modelObject_->position();
pixmapSize_ = modelObject_->pixmap()->size();

setPos(pos);
originalOffset_ = pos;

setZValue(1);
// setTransformationMode(Qt::SmoothTransformation);

selectionItem_ = new QGraphicsRectItem(
pos.x() - 2, pos.y() - 2, pixmapSize_.width() + 4,
pixmapSize_.height() + 4, 0, modelScene);
selectionItem_->setVisible(false);
QPen p(Qt::red);
selectionItem_->setPen(p);
selectionItem_->setZValue(5);

QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect();
shadow->setOffset(QPointF(3, 3));
setGraphicsEffect(shadow);

labelMetrics_ = new QFontMetrics(QFont());
labelWidth_ = 0;
labelHeight_ = labelMetrics_->height();
labelVisible_ = true;

updateItem();
}
The text labels are drawn/shown as follows (the painter->drawText(...)):

void ModelObjectGraphicsItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0)
{
// Paint the pixmap item
QGraphicsPixmapItem::paint(painter, option, widget);

// Paint the label (if visible)
QPoint labelPos((pixmapSize_.width() / 2) - (labelWidth_ / 2), pixmapSize_.height() + labelHeight_);
if (labelVisible_)
painter->drawText(labelPos, labelText_);
}
Any ideas on how I can "drop shadow" only from the "boxes"?

wysota
15th January 2010, 15:14
Make the box and the label separate items.


QGraphicsItemGroup *myItem = new QGraphicsItemGroup;
BoxItem *box= new BoxItem(myItem);
LabelItem *label = new LabelItem(myItem);
scene()->addItem(myItem);

Then you can apply the effect on one of the sub-items only.