Results 1 to 7 of 7

Thread: MouseDoubleClickEvent on a QGraphicsScene

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default MouseDoubleClickEvent on a QGraphicsScene

    Hi,
    I've a question...

    I use QGraphicsView. I've this method on a sublcasses of QGraphicsScene:

    Qt Code:
    1. void MPApplicationScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if(items(event->scenePos()).last()!=0)
    4. {
    5. emit itemSelected(items(event->scenePos()).last());
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    In my QGraphicsScene I've more than one item. One of this kinds is a subclasses of QGraphicsItem and other are QGraphicsRectItem. The QGraphicsItem item have a z value more little (for this I use the last() function of the item list obtained with the items() QGraphicsScene function).

    How can I be sure that the items that I double clicked are instance of the subclasses of the QGraphicsItem and not other (QGraphicsRectItem for example)?!?

    Is there any method to understand which kind of instance is a specify object?!?!

    Thanks

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    Using standard C++, you can use dynamic_cast :
    Qt Code:
    1. YourGraphicsItem * item = dynamic_cast<YourGraphicsItem*>(items(event->scenePos()).last());
    2. if(item) {
    3. //do your stuff
    4. }
    To copy to clipboard, switch view to plain text mode 

    I know Qt provides its own object cast, but I don't use them

  3. #3
    Join Date
    Oct 2009
    Posts
    70

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    I use this solution but if the item double clicked is a QGraphicsRectItem, the item is true and the application enter the If(item)...

    I need something that can ensure me that the item is a MPGraphicsItem (my subclasses of QGraphicsItem)... the dynamic cast it isn't not enough..

    Thanks

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    I use this solution but if the item double clicked is a QGraphicsRectItem, the item is true and the application enter the If(item)...
    It shouldn't.

    Maybe last() is not the method you need. Try first().
    But what's going in if the list returns by items is empty?

    So, simply use QGraphicsView::itemAt() to get the topmost item.

  5. #5
    Join Date
    Oct 2009
    Posts
    70

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    No..I try to explain better...

    I've a scene where a put different kind of items:

    1) MPGraphicsItem derived from QGraphicsItem (with the lowest zValue);
    2) QGraphicsRectItem with intermediate zValue
    3) QGraphicsLineItem whit the bigger zValue

    The items() returns a list with all the item of a x,y,z position from the bottom to the top...

    The problem is this: When I double click on the scene I want to execute one specify method only if on the position x,y,z there is a MPGraphicsItem.

    The dynamic cast only cast the item selected with MPGraphicsItem but this action works with other item too (if the last item is a QGraphicsLineItem the cast works and the if(item) returns true).

    This cause it's possible that in one x,y,z position there is only a QGraphicsLineItem.

    I need to be sure that the QGraphicsItem that the mouseDoubleClickEvent returns it's a MPGraphicsItem...this also after the dynamic cast...

    I hope that this explanation is more clear.

  6. #6
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    I don't really understand what you want to do with the items under the mouse,
    but what I can ensure you is, according to C++ coding, dynamic_cast should work.

    Quote Originally Posted by paolom View Post
    The dynamic cast only cast the item selected with MPGraphicsItem but this action works with other item too
    The return value of dynamic_cast is not a boolean, this is the adress of your item down-casted, resetted to null if not compliant with the type.
    Either you are not using it properly or you class hierarchy is not what you think.

    Quote Originally Posted by paolom View Post
    The items() returns a list with all the item of a x,y,z position from the bottom to the top...
    The docs says about QGraphicsScene::items :
    Returns all visible items at position pos in the scene. The items are listed in descending Z order (i.e., the first item in the list is the top-most item, and the last item is the bottom-most item).

    What are the classes inheritance you defined and what is the code you says dynamic_cast returns true?

  7. #7
    Join Date
    Oct 2009
    Posts
    70

    Default Re: MouseDoubleClickEvent on a QGraphicsScene

    I've solved...I've make a code error...it works with the dynamic_cast

    Thanks!!

Similar Threads

  1. QGraphicsScene core dump?
    By cookie1909 in forum Newbie
    Replies: 2
    Last Post: 25th April 2009, 07:06
  2. [QGraphicsView] Auto adjusting QGraphicsScene
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2009, 22:31
  3. Replies: 0
    Last Post: 5th March 2009, 06:54
  4. QPixmap display on QGraphicsScene
    By febil in forum Qt Programming
    Replies: 2
    Last Post: 26th February 2009, 09:27
  5. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27

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.