I can't find where I'm doing wrong. I want to change the color of my item (a QGraphicsRectItem) when a particular event occurs. The fact is that it seems that once the override paint method is called, the color won't change no matter what. This is a simplyfied code of what I've done:
item.h
class Item : public QGraphicsRectItem
{
public:
Item(QGraphicsView *graphView);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) override;
private:
QPointF newPos;
QGraphicsView *graph;
};
item.cpp
Item::Item(QGraphicsView *graphWidget) : graph(graphWidget) { }
void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::black);
painter->drawEllipse(-7, -7, 20, 20);
}
main.cpp
int main(int argc, char *argv[])
{
srand(QDateTime::currentDateTime().toMSecsSinceEpoch());
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
Item *item = new Item(&view);
scene.addItem(item);
item->setPos(0, 0);
item->setBrush(Qt::red);
item->update();
view.show();
return a.exec();
}