I have a QGraphicsScene, QGraphicsView and some QGraphicsItems subclassed. I want to track which item is under the cursor topmost visible. It works fine using hoverEnterEvent in most situations, but if I have two Objects where one is on top of another it does work on entering both but not on leaving the inner object (and re-entering the outer, since it never left the outer in the first place).
           +-------------------------------------+
  outside  |                                     |
           |  outer                              |
           |                                     |
           |                                     |
           |           +-------------+           |     +-------------+ 
           |           |             |           |     |             | 
           |           |             |           |     |   another   |
           |           |    inner    |           |     |             |
           |           |             |           |     |             |
           |           |             |           |     +-------------+ 
           |           +-------------+           |
           |                                     |
           |                                     |
           |                                     |
           |                                     |
           +-------------------------------------+
outside -> outer : works, outer is selected
outside -> outer -> outside -> another : works, first outside is selected, than nothing, than another
outer -> inner : works, inner is seletected
outside -> outer -> inner -> outer: does not work, first outside is selected, than inner, but than nothing (should be outer again)
What can I do, besides looping trough all graphicitems triggered via a slight delayed singleshot on hoverLeaveEvent?
Edit: I found a temporary solution: I added a global QList < MyQGraphicsItem *> where on MyQGraphicsItem::hoverEnterEvent I add "this", and on MyGraphicsItem::hoverLeaveEvent I remove the last item in the List. So the myGlobalQList.last() always contains the topmost item visible under the cursor. I assume this is not the best solution since QList is not threadsafe, therefor I am still interested in other solutions.
QList<QGraphicsItem*>solution isn't that bad after all - yourQGraphicsSceneisn't thread-safe itself, so you don't have to worry aboutQList. Semantically you'd use aQStack(LIFO), but generally I recommend to try usingQGraphicsScene::itemAt(QPointF pos)instead. That's fast enough and you don't need to worry about keeping yourQListup to date. - Martin Hennings