Results 1 to 4 of 4

Thread: QGrsphicsItem click and hover events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGrsphicsItem click and hover events

    hi

    I need to create an inherited class of QGraphicsItem that implements the mouse click and hover events in it, can anybody give a simple example of how to do this??

    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGrsphicsItem click and hover events

    Simple reimplement the event handlers: QGraphicsItem::mousePressEvent(), QGraphicsItem::mouseReleaseEvent() and QGraphicsItem::mouseMoveEvent(), but for the last you have to set the corresponding flag to receive these events.

  3. #3
    Join Date
    Oct 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGrsphicsItem click and hover events

    Here is an example:

    Qt Code:
    1. #include <QGraphicsItem>
    2.  
    3. class Test : public QGraphicsItem
    4. {
    5. Q_OBJECT
    6. public:
    7.  
    8. // Konstruktor
    9. Test(QGraphicsItem *parent = 0) :
    10. QGraphicsItem(parent)
    11. {
    12. // initialize
    13. }
    14.  
    15. protected:
    16. /**
    17.   * Event: Mousbutton is released ( = Clicked)
    18.   * @param event
    19.   */
    20. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
    21. // do something
    22.  
    23. // call the parents's event
    24. QGraphicsItem::mouseReleaseEvent(event);
    25. }
    26.  
    27. /**
    28.   * Mousecursor enters Widget.
    29.   */
    30. void hoverEnterEvent(QGraphicsSceneHoverEvent *event){
    31.  
    32. // do something
    33.  
    34. QGraphicsItem::hoverEnterEvent(event);
    35. }
    36.  
    37. /**
    38.   * Mousecursor leaves Widget
    39.   */
    40. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    41.  
    42. // do something
    43.  
    44. QGraphicsItem::hoverLeaveEvent(event);
    45. }
    46.  
    47. /**
    48.   * Mousecursor is moved in Widget.
    49.   */
    50. void hoverMoveEvent(QGraphicsSceneHoverEvent *event){
    51.  
    52. // do something
    53.  
    54. QGraphicsItem::hoverMoveEvent(event);
    55. }
    56. };
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Ceelaz for this useful post:

    liqxpil (21st October 2010)

  5. #4
    Join Date
    Oct 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGrsphicsItem click and hover events

    great thanks guys

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. Click events
    By Maluko_Da_Tola in forum Newbie
    Replies: 0
    Last Post: 26th August 2010, 01:05
  3. Handling mouse click events in QGraphicsItem
    By Luc4 in forum Qt Programming
    Replies: 7
    Last Post: 5th March 2010, 16:12
  4. QMenuBar hover/click clears QStatusBar in QMainWindow
    By killerwookie99 in forum Qt Programming
    Replies: 0
    Last Post: 16th November 2009, 15:09
  5. QGraphicsItem hover events
    By stefan in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 11:01

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
  •  
Qt is a trademark of The Qt Company.