Results 1 to 8 of 8

Thread: Handling mouse click events in QGraphicsItem

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Handling mouse click events in QGraphicsItem

    Hi! I'm trying to handle to mouse click event in a subclass I wrote of QGraphicsItem. The problem is that I'm able to handle correctly mousePressEvent and mouseReleaseEvent, but not clicks. I really need clicks cause I also use drag mode, so I need that, in case the user presses the mouse button, but releases after a while or drag and then releases, the press doesn't have to be interpreted as a click. Do I have to implement this myself with a timer?
    Thanks!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse click events in QGraphicsItem

    If you want clicks, then yes, you have to implement the logic yourself. QGraphicsItem doesnt have it.

    Also if you just want to drag your items, you can do it simply by setting proper QGraphicsItem flags. You need not re-implement the wheel

  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handling mouse click events in QGraphicsItem

    I tried to do this, but it is quite difficult it seems... If I handle the pressEvent to start a timer and then to do whatever it does usually, I get that no releaseEvent is thrown afterwards... Could you confirm this? So, I cannot understand whether this was a click or not... I also tried to check if a mouseMove was thrown, as this would result in that not being a click, but it seems I can't call the function of the superclass to allow the drag in the QGraphicsView... Is there any other way to implement the click event?
    Thanks!

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse click events in QGraphicsItem

    Do you want the click ? Or you want dragging items ?
    May be if you tell your requirement we can reply you better

  5. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handling mouse click events in QGraphicsItem

    Oh, sorry. I wasn't clear. I don't want to drag items, but I want to be able to use the setDragMode to QGraphicsView::ScrollHandDrag while handling the event of clicking on an item. If the user clicks on an item, I want to do something, but in case the user presses, but moves without releasing, I want the the graphicsview to scroll as if the user pressed where no item was present. Thank you for your time!

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling mouse click events in QGraphicsItem

    Well, you can do one thing... inherit the item, and ignore the mouse move events when the mouse is pressed.
    And I guess it wont be difficult to know if the mouse was pressed, isnt it ?
    Also check the chip demo in Qt demos, if you press shift after mouse press over item, you can see lines are drawn over it. similary you can ignore the events so that the view processes them and drag might work in that case .

  7. #7
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handling mouse click events in QGraphicsItem

    I still can understand how to make this work... Ignoring mouseMove events mean that I need to create a empty function in my subclass?

  8. #8
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Handling mouse click events in QGraphicsItem

    Ok, I made it work. Just in case anyone else needs this, I did it by subclassing the QGraphicsScene and reimplementing there mousePressEvent and mouseReleaseEvent and using a QTime. I wasn't able to do that subclassing QGraphicsItem cause it seems mouseReleaseEvent is not called. Now it seems to work quite good.

Similar Threads

  1. How to get mouse click events outside the Qt window?
    By montylee in forum Qt Programming
    Replies: 11
    Last Post: 13th July 2015, 21:55
  2. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 14:15
  3. can not get mouse double click event for QGraphicsItem
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2009, 20:36
  4. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 11:19
  5. Handling Mouse Events of a parent widget
    By dvmorris in forum Qt Programming
    Replies: 2
    Last Post: 28th March 2007, 18:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.