Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    2
    2
    7039
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      GoldenAxe last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • M
        MaQzma last edited by

        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

        Mario.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post