Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QGraphicsScene hover*Events to all item under mouse cursor.



  • Hello!
    I am making an application that is similar to the Diagram Scene Example. I expanded it by adding a resizer item(SizeGripItem) and in the future, I want to add a slightly similar element in the structure. And they all use hover*Events.
    The problem is that both of these items(SizeGripItem and another "future item") are child of the DiagramItem, and they lie on top of the parent. So all hover*Events are received by the element that was added last. In fact, they all get it, but in the wrong way. For example, hoverMoveEvent works only once when it enters the child's shape. Which is not typical for this event. I tried this option from the StackOverflow post. It says that you need to redefine QGraphicsView or QGraphicsScene mouseMoveEvent and send events using QGraphicsScene::sendEvent, but it seems to me that this is a bit of a crutch way. Because as I understood it, I would have to implement the full logic of all hover events from scratch. I also tried to call event->ignore(), but the documentation says that this has no effect.

    The looks of the DiagramItem with SizeGripItem and "future item" when it's hovered.
    Screen Shot 07-22-21 at 11.07 PM.PNG

    The looks of the DiagramItem with SizeGripItem and "future item" when it's
    not hovered.

    Screen Shot 07-22-21 at 11.31 PM.PNG

    Thanks for any help or suggestions that will help solve this problem.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What about reverting the order of the items ?



  • @SGaist Please excuse me if I misunderstand something. But as I understand it, you want me to make children as the parent of the DiagramItem? That is:

    From this:
    DiagramItem
    |_SizeGripItem
    |_"future item"

    To this:
    SizeGripItem
    |_DiagramItem

    "future item"
    |_DiagramItem

    But is that really to have more than one parent? Sorry for not showing the hierarchy initially.


  • Lifetime Qt Champion

    I meant to stack them differently, so future on top of size grip on top of main item.

    Are you grouping them ?



  • This post is deleted!


  • @SGaist No, I'm not grouping them, just parenting. Here is the code of how item is created. ArrowManager is "future item". Just temporary name.

    DiagramItem *DiagramScene::createDiagramItem(int diagramType)
    {
        DiagramItem* diagramItem = new DiagramItem(DiagramItem::DiagramType(diagramType));
    
        SizeGripItem* sizeGripItem =
                new SizeGripItem(new PathResizer, diagramItem);
    
        new ArrowManager(diagramItem);
    
        connect(sizeGripItem, &SizeGripItem::resizeBeenMade,
                diagramItem,  &DiagramItem::updateTextItemPosition);
    
        connect(diagramItem, &DiagramItem::itemPositionChanged,
                this,        &DiagramScene::drawLevelLineWithItemOnSameAxis);
    
        connect(diagramItem, &DiagramItem::itemReleased,
                this,        &DiagramScene::deleteAllLevelLines);
    
        return diagramItem;
    }
    

  • Lifetime Qt Champion

    Out of curiosity, why are you making three different items for what seems to be only one in the end ?



  • @SGaist It seemed to me that this is the easiest and most convenient option, that is, to manage everything separately. Yes, maybe this is not the best option, but due to my inexperience, nothing better comes to my mind except this option.


  • Lifetime Qt Champion

    Can you share a minimal compilable example ?

    That will allow to better understand your implementation and what is happening.



  • @SGaist Of course, but it will take some time. How is it convenient for you to provide the code? Should I leave the pieces of code here in this way:

    DiagramItem.h

    // DiagramItem definition code
    // ...
    

    DiagramItem.cpp

    // DiagramItem implementation code
    // ...
    

    etc

    // ...
    

    or in some other way?

    Thank you so much for your support!


  • Lifetime Qt Champion

    Either that or if you have a GitLab/GitHub/BitBucket/Other repo.



  • @SGaist Here is a minimal example. I don't know if the example is small enough. But I tried to make it as small as possible.


Log in to reply