PDA

View Full Version : MouseDoubleClickEvent on a QGraphicsScene



paolom
6th October 2009, 10:59
Hi,
I've a question...

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


void MPApplicationScene::mouseDoubleClickEvent(QGraphic sSceneMouseEvent *event)
{
if(items(event->scenePos()).last()!=0)
{
emit itemSelected(items(event->scenePos()).last());
}
}

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

scascio
6th October 2009, 11:09
Using standard C++, you can use dynamic_cast :

YourGraphicsItem * item = dynamic_cast<YourGraphicsItem*>(items(event->scenePos()).last());
if(item) {
//do your stuff
}

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

paolom
6th October 2009, 11:16
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

scascio
6th October 2009, 11:27
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.

paolom
6th October 2009, 11:35
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.

scascio
6th October 2009, 11:55
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.



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.



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?

paolom
6th October 2009, 12:01
I've solved...I've make a code error...it works with the dynamic_cast

Thanks!!