Custom QGraphicsItem mousePressEvent for single click works only when double click [Solved]



  • Hi all,

    I have the following mousePressEvent, it's works fine but only when double clicking the mouse, I want it to work only when single click on the mouse button.
    @void MapNode::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    isClicked *= -1;
    QGraphicsItem::mousePressEvent(event);
    update();
    }@
    Thanks

    I found a solution for this:
    I have a mousePressEvent in the parent of my custom QGraphicsItem, which is a custom QGraphicsView, only needed to add inside the custom QGraphicsView mousePressEvent the following line of code in the beginning of the method:
    @QGraphicsView::mousePressEvent(event);@
    so now single click works fine, probably the event on the parent blocked the event on the child.



  • Hi,

    Every time that you override those virtual methods (mouseDoubleClickEvent, mouseMoveEvent, mousePressEvent...) you need to call base class metod too.
    @
    QGraphicsScene::mousePressEvent(event);
    @

    And you don't need to call:
    @
    QGraphicsItem::mousePressEvent(event);
    @

    So, works with:
    @
    QGraphicsView::mousePressEvent(event);
    @

    because

    bq. The view receives input events from the keyboard and mouse, and translates these to scene events (converting the coordinates used to scene coordinates where appropriate), before sending the events to the visualized scene.

    http://doc.qt.digia.com/qt/graphicsview.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.