Results 1 to 6 of 6

Thread: How to handle QGraphicsItem mouse events in QGraphicsView

  1. #1
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default How to handle QGraphicsItem mouse events in QGraphicsView

    hi all,
    I wrote some objects derived from QGraphicsView and QGraphicsItem, and I simulate a mouse click event in my QGraphicsItem just like this
    Qt Code:
    1. void QMyItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
    2. {
    3. // if(e->buttons() == Qt::LeftButton)
    4. // SetButtonState(NORMAL);
    5.  
    6. QGraphicsPixmapItem::mousePressEvent(event);
    7.  
    8. setSelected(false); // i don't need the focus
    9.  
    10. QPointF lastpt = event->lastPos();
    11. QRectF bRect = boundingRect ();
    12.  
    13. if(bRect.contains(lastpt))
    14. ; // ??? It's a click event.
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    My question is, how can i track the mouse 'click' event in my QGraphicsView? in other words, i need to integrate these mouse events in QGraphicsView and find which item activate it,
    Qt Code:
    1. void QMyGraphicsView::Command( param)
    2. {
    3. switch(param)
    4. {
    5. case ?? : ...
    6. case ?? : ...
    7. }
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    or is there a better place to dispose other than QGraphicsView?

    thanks for any advice.
    Last edited by HiJack; 27th April 2010 at 07:24.

  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: How to handle QGraphicsItem mouse events in QGraphicsView

    My question is, how can i track the mouse 'click' event in my QGraphicsView?
    For that you will need to properly handle mouse press and mouse release event of the graphics item and then emit a signal from the item.
    Thats how things are done in normal pushbuttons also. But for graphics item, you will need to decide when you want to emit the clicked() signal... on mouse press, on mouse release, duration between the press and release, etc.

  3. #3
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to handle QGraphicsItem mouse events in QGraphicsView

    One good place to do it is in the QGraphicsScene, QGraphicsScene::mouseGrabberItem() will return the item that accepted a mousePressEvent (at the release it will not return it though) .

    Another one is QGraphicsScene::focusItem() which returns the item that is focused.

    Or in the QGraphicsView, when you got mouseReleaseEvent get the mouse position and then pass it to QGraphicsView::itemAt(const QPoint & pos).

    You can do the same in QGraphicsScene with QGraphicsScene::itemAt(...) (will return the topmost visible item at that position) or QGraphicsScene::items(...) (you'll get all the items at that position)
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

  4. #4
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to handle QGraphicsItem mouse events in QGraphicsView

    Thank you guys.
    QGraphicsItem is not QObject-based , but how i emit the signal to its parentview? I'll try 'QGraphicsScene::mouseGrabberItem()' first.
    I'm a two-days Qt'er, thanks for your reply.

  5. #5
    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: How to handle QGraphicsItem mouse events in QGraphicsView

    QGraphicsItem is not QObject-based , but how i emit the signal to its parentview?
    Make a new class where you inherit from graphics item and QObject

    And if you have Qt 4.6.x, have a look at QGraphicsObject

  6. #6
    Join Date
    Apr 2010
    Posts
    24
    Thanks
    5
    Qt products
    Qt4

    Default Re: How to handle QGraphicsItem mouse events in QGraphicsView

    Quote Originally Posted by aamer4yu View Post
    Make a new class where you inherit from graphics item and QObject

    And if you have Qt 4.6.x, have a look at QGraphicsObject
    appreciate, I will do that.

Similar Threads

  1. Handle Drag & Drop Events with QGraphicsItem [SOLVED]
    By guilugi in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2010, 18:05
  2. QGraphicsItem no mouse events called
    By munna in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2009, 14:43
  3. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 14:15
  4. QGraphicsItem mouse events
    By zgulser in forum Qt Programming
    Replies: 13
    Last Post: 11th February 2009, 11:19
  5. QPushButton:: Handle right mouse button events. Easyway?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2007, 16:56

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.