PDA

View Full Version : Rubberband Item Selection



zgulser
22nd October 2009, 09:38
Hi all,

I implemented my own rubberband to perform zooming. I'm done with all zooming stuff but selecting. I wrote the following code to do selection;



void Scene::performRubberbandSelection(QRectF* pRect)
{
QList<QGraphicsItem*> itemList = items(*pRect, Qt::IntersectsItemShape);

if(itemList.size() == 0)
return;

for( int i=0; i<itemList.size();i++)
{
if(itemList.at(i)->type() == 7)
{
continue;
}
else
cout<<itemList.at(i)<<endl;//to test


itemList.at(i)->setSelected(true);
}
}


but the problem is in items(...) method. it returns only one root item but not the others in the rubberband rectangle. What could go wrong here?

Thanks in advance

scascio
22nd October 2009, 09:47
Maybe a problem conversion coordinates, or offset.

Is pRect expressed in scene coordinates?

zgulser
22nd October 2009, 11:24
Hımm, you are right. it seems rectangle points defined it's local coordinate system. how could I fix this?

But let me remind you, itemlist is the problematic. I mean there is a problem before finding the intersections.

scascio
22nd October 2009, 12:20
The use of QGraphicsScene::items requires that the rect passed in argument is in scene coordinate.

So, check where your rect is computed,
then use QGraphicsView::mapToScene to convert from mouse position to scene position or QGraphicsItem::mapRectToScene from local coordinates to scene coordinates.

zgulser
23rd October 2009, 07:58
Hi,

I check the coordinates of the rectangle and I found it's top left point is (0,0). In order to get rid of this local coordinates, I used mapToScene() but it still returns (0,0). But what I don't understand is how the view map the QRectF's points which are not belong to the scene.

scascio
23rd October 2009, 08:48
Your Scene::performRubberbandSelection seems right.

I think the problem resides in the way you are computing the rect before passing it to the method.

So, to sum up :

* if you are getting cursor position from QMouseEvent in a mouse event of the view use QGraphicsView::mapToScene. This will convert pixel coordinates of the viewport into scene coordinates (it is a widget)
* if you are computing the rect from an item (why not?) from a QGraphicsSceneMouseEvent in an item mouse event, use QGraphicsItem::MapToScene to convert from local coordinate to scene coordinates.


In the case you dont get rid of this, just post the code of the method involved in the computation of your rubberband from the mouse events to the your performRubberbandSelection()