PDA

View Full Version : update QGraphicsPolygonItem qbrush in mousePressEvent method



thgis
1st October 2010, 10:45
Hi.

I'm trying to change the brush on an QGraphicsItem (More precise my own class that inherits from QGraphicsPolygonItem).

My reimplementation of mousePressEvent looks like this


void BlobItem::mousePressEvent(QGraphicsSceneMouseEvent * event)
{
std::cout << "label: " << _label->label << endl;
if (!_label->label.compare("a")) {
QBrush brush;
QColor color("brown");
brush.setColor(color);
setBrush(brush);
_label->label = "b";
this->setZValue(parentItem()->zValue() + 1);
} else if (!_label->label.compare("b")) {
QBrush brush;
QColor color("black");
brush.setColor(color);
setBrush(brush);
_label->label = "a";
this->setZValue(parentItem()->zValue() + 1);
}
update(boundingRect());
QGraphicsPolygonItem::mousePressEvent(event);
event->ignore();
}

I can see that each time I press the mouse button on an item the label toggles between 'a' and 'b'. My items originally have a brush with a blue color therefore they are initially painted with a blue interior, but when I click on them not only does the color not change to the one specified in the if statements, the original blue color disappears and the background (the parent item) gets visible.

Can someone see what I'm doing wrong?

Any help will greatly appreciated

Regards,
Thomas

qlands
1st October 2010, 15:44
Hi,

Maybe you need to set the style of the new brush to solid:

brush.setStyle(Qt::SolidPattern);

thgis
1st October 2010, 15:57
thank you so much... It has been bothering me a while.
Now it works as expected..:)