Qt 5: QGraphicsItem update() mechanism
-
(Qt 5.4 on Win system)
Hello Qt'ies!
I have a QGraphicsScene, which can be zoomed and panned. In the picture attached there's a gray line which is a zoomed-in view of arc (ArcGraphItem is a QGraphicsItem-derived class).
The two yellow lines beside are helper lines painted to demonstrate the clicking/picking area of the arc (implementation in shape() method).void ArcGraphItem::paint( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { qDebug() << "repaint"; ArcGraphItem::scaleFactor = painter->transform().m11(); painter->save(); if (this->hovered) { painter->setPen(AbstractGraphItem::hoverPen); } else if (this->selected) { painter->setPen(AbstractGraphItem::selectPen); } else { painter->setPen(AbstractGraphItem::unselectPen); } /* draw arc by start/end point incl. re-connection to end point because of glitchy 1/16 integer angles */ QPainterPath path(this->startPoint); path.arcTo(this->rectangle, this->startAngle, this->spanAngle); path.lineTo(this->endPoint); painter->drawPath(path); #if SHOW_PICK_AREA == 1 /* yellow outline of the picking area */ painter->setPen(AbstractGraphItem::pickAreaPen); painter->drawRect(this->boundingBox); painter->drawPath(this->shape()); #endif painter->restore(); }
When clicking the arc, I call a selection method, which calls update() method of the AbstractGraphItem:
void ArcGraphItem::setSelected( bool selected) { if (this->selected != selected) { this->selected = selected; qDebug() << (selected ? "" : "not") << "select"; this->update(); } }
The debug output clearly show that setSelected() is called and toggles the selection status, which is interpreted in paint() method. Unfortunately, the update() does not trigger a call to paint(), if only this single AbstractGraphItem is visible at the moment. If I a add another QGraphicsItem to the scene ("small point"), then the paint() method of the clicked arc is triggered.
According to a thread on StackOverflow [http://stackoverflow.com/questions/22900106/qt-update-doesnt-work#] I also have to call the update method of the scene. Do I have to?
Thank you for reading,
Riff RaffBtw: shape() and boundingRect() methods are impl'd for ArcGraphItem.