Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom QGraphicsItem mousePressEvent for single click works only when double click [Solved]
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 7.2k Views 1 Watching
  • 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 Offline
    G Offline
    GoldenAxe
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      MaQzma
      wrote on last edited by
      #2

      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
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved