if you want to change the color of the item upon selection then restore the color again upon de-selection:
Set ItemIsSelectable flag in Graphics::Graphics()
setFlag(ItemIsSelectable);
setFlag(ItemIsSelectable);
To copy to clipboard, switch view to plain text mode
then in Graphics::paint() change the color of the brush if the item is selected
if (isSelected())
painter->setBrush(Qt::black);
else
painter->setBrush(Qt::green);
if (isSelected())
painter->setBrush(Qt::black);
else
painter->setBrush(Qt::green);
To copy to clipboard, switch view to plain text mode
if you want to change the color of the item upon selection forever:
no flags, just reimplement mousePressEvent()
{
if (brushcolor == Qt::green)
brushcolor = Qt::black;
update();
}
void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (brushcolor == Qt::green)
brushcolor = Qt::black;
update();
QGraphicsItem::mousePressEvent(event);
}
To copy to clipboard, switch view to plain text mode
then your Graphics::paint() should set the brush color:
painter->setBrush(brushcolor);
painter->setBrush(brushcolor);
To copy to clipboard, switch view to plain text mode
and in your constructor:
brushcolor = Qt::green;
brushcolor = Qt::green;
To copy to clipboard, switch view to plain text mode
also, don't forget to define brushcolor as member of Graphics class:
private:
...
Qt::GlobalColor brushcolor;
private:
...
Qt::GlobalColor brushcolor;
To copy to clipboard, switch view to plain text mode
i tried both, and they worked fine with me. except the second method, two items are selected at a time. i think you have something strange in your bounding rect or i did something wrong.
Regards,
-Mustafa
Bookmarks