Results 1 to 6 of 6

Thread: Find the item under the mouse press

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Find the item under the mouse press

    Hello forum,


    I am over-ridden the mousePressEvent(QGraphicsSceneMouseEvent) of the QGraphicsScene and doing the following to find if there is any item under the mouse press or not:


    Qt Code:
    1. QGraphicsItem *item = itemAt(event->pos());
    2.  
    3. if(item)
    4. cout << "Item found under the mouse press " << endl;
    To copy to clipboard, switch view to plain text mode 

    When i drag the item into the scene i can see that there is an item, the mousePressEvent() is not detecting it. Is there any basic logic that i am missing ?


    Thanks
    Sajjad

  2. #2
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Find the item under the mouse press

    Not sure this is the problem but in case you have subclassed QGraphicsItem, make sure that the boundingRect() of your graphics item is correct.

  3. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Find the item under the mouse press

    Quote Originally Posted by helloworld View Post
    Not sure this is the problem but in case you have subclassed QGraphicsItem, make sure that the boundingRect() of your graphics item is correct.

    I think the boundingRect() is fine. I am trying to extend the demo that comes along with the Qt installation - diagramscene.
    The examples already over-rides the mousePressEvent() and i have just added the condition as mentioned in the previous post at the end of the function.


    Any other hint?



    Regards
    Sajjad

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Find the item under the mouse press

    Use a qDebug() statement:
    Qt Code:
    1. qDebug() << event->pos()
    To copy to clipboard, switch view to plain text mode 
    event->pos() is not what you want. Look in the docs for alternatives.

    Quote Originally Posted by sajis997 View Post
    Any other hint?
    On what type of object you are calling itemAt()?

  5. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Find the item under the mouse press

    Quote Originally Posted by norobro View Post
    Use a qDebug() statement:
    Qt Code:
    1. qDebug() << event->pos()
    To copy to clipboard, switch view to plain text mode 
    event->pos() is not what you want. Look in the docs for alternatives.


    On what type of object you are calling itemAt()?

    Qt Code:
    1. DiagramItem *item = qgraphicsitem_cast<DiagramItem*>(itemAt(event->pos()));
    To copy to clipboard, switch view to plain text mode 

    And the event type is the QGraphicsSceneMouseEvent .

    [/code]

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Find the item under the mouse press

    You are trying to locate an item in a scene so try:
    Qt Code:
    1. QGraphicsItem *item = itemAt(event->scenePos())
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to norobro for this useful post:

    sajis997 (18th August 2011)

Similar Threads

  1. Replies: 1
    Last Post: 17th August 2011, 17:44
  2. QlistView mouse press
    By dima in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2010, 15:51
  3. QGraphicsview and mouse press event
    By eva2002 in forum Qt Programming
    Replies: 6
    Last Post: 26th January 2010, 05:04
  4. QTreeView: Find nearest item to mouse pointer
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 31st October 2009, 20:24
  5. Replies: 1
    Last Post: 24th October 2007, 18:34

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.