Highlighting a QGraphicsItem in a QGraphicsScene
maikelmeyers last edited by
I want to highlight a GraphicsRectItem in a QGraphicsscene if the mouse is over this item. For this purpose I reimplemented paint() of the item, asking for "option->state & QStyle::State_MouseOver". This works but the item keeps highlighted while the mouse button is hold down, even if the mouse is not over the item anymore and stops beeing highlighted if the mouse button is released. Is there any possibilty that the State_MouseOver-Flag is unset if the mouse is not over the item, even if the mouse button is hold down?
void PolygonLineHandle::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *)
if (option->state & QStyle::State_MouseOver)
This is not a definitive answer, just ideas.
Study "mouse grab". When a mouse button is pressed in a widget, it grabs the mouse. Same thing for QGI's. The idea is that further mouse events are dispatched to the widget that has grabbed the mouse, even if the mouse leaves the widget. The design reasoning is that only that widget knows what to do with the mouse events. I think the concept of mouse grab is somewhat controversial, but it is in Qt. So my guess is that mouse grab affects the state and changes the way it is updated: doesn't update mouseOver???
Also, have you looked at hover events?
I need the same effect, a "highlight" of an item different from the automatic highlighting that Qt does when you select (or focus>) a QGI. But I will have a state variable in my class that I will maintain to indicate it should be highlighted.
You can reimplement hoveEvents. Somethin like this:
class HighlightItem : public QGraphicsRectItem
explicit HighlightItem(QGraphicsRectItem *parent = 0);
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
#endif // HIGHLIGHTITEM_H
HighlightItem::HighlightItem(QGraphicsRectItem *parent) :
void HighlightItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
void HighlightItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
void HighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)