PDA

View Full Version : Restrict Boundary for QGraphicsPolygonItem



sean.reynolds
10th April 2011, 08:37
Hey guys,

I havea a QGraphicsPolygonItem (Just a simple rectangle) that I can drag around in my QGraphicsScene.

I can select the shape and drag it around. However if it is dragged too far to the edge of the window, it gets clipped and goes off screen.

Have a look at the API but couldn't find how I can restrict the area the polygon can move around.

Any ideas?

Thanks

Sean

sean.reynolds
10th April 2011, 12:07
Found the solution to the problem.



public void mouseMoveEvent(QGraphicsSceneMouseEvent event)
{
super.mouseMoveEvent(event);

if (movingBlock != null)
{
if(movingBlock.pos().x() <= 0.0)
{
movingBlock.setPos(0.0, movingBlock.y());
}

if(movingBlock.pos().x() >= this.sceneRect().width())
{
movingBlock.setPos(this.sceneRect().width(), movingBlock.y());
}

if(movingBlock.pos().y() <= 0)
{
movingBlock.setPos(movingBlock.x(), 0.0);
}

if(movingBlock.pos().y() >= this.sceneRect().height())
{
movingBlock.setPos(movingBlock.x(), this.sceneRect().height());
}
}
}