PDA

View Full Version : Re-Implementation of QGraphics::itemChange() NOT working



ayanda83
19th April 2015, 08:49
hi guys, I have a graphicsItem that is a child item of another graphicsItem which is basically a filled rectangle. the child item is movable and I want to limit the movement of the child item to the bounds of the parent item. The bounding rect of the parent is the same size as the scene rect hence I used the scene rect to define the bounds in the code below. But my code below doesn't work and the child item still goes upside the parent item bounds when you drag move it. I want the item to stop at the scene bounds or the parent item bounds (which are the same rect size). please see code below.
QVariant Picture::itemChange(QGraphicsItem::GraphicsItemCha nge change, const QVariant &value)
{
if(change == ItemPositionChange && scene()){
QPointF newPos = value.toPointF();
QRectF rect2 = scene()->sceneRect();

if(!rect2.contains(newPos)){
// Keep the item inside the scene rect.
newPos.setX(qMin(rect2.right(), qMax(newPos.x(), rect2.left())));
newPos.setY(qMin(rect2.bottom(), qMax(newPos.y(), rect2.top())));

return newPos;
}

}
return QGraphicsItem::itemChange(change, value);
}


thanking you in advance.

anda_skoa
19th April 2015, 09:42
The parent's rectangle from a child's point of view is (0, 0, parent.width, parent.height).

Alternatively you can map your position into scene coordinates.

Cheers,
_

ayanda83
19th April 2015, 11:46
The parent's rectangle from a child's point of view is (0, 0, parent.width, parent.height).

Alternatively you can map your position into scene coordinates.

Cheers,
_

I am not sure if I am doing this right but I have done this
return mapToScene(newPos); to mapToScene coordinates and this doesn't work either. But may have done something wrong. any suggestions?

anda_skoa
19th April 2015, 12:41
I am not sure if I am doing this right but I have done this
return mapToScene(newPos); to mapToScene coordinates and this doesn't work either. But may have done something wrong. any suggestions?

How would that help?

As I said, you can either do the check in the item's coordinate space or in the scene's coordinate space.
Mixing the two is not likely to have the correct results.

Cheers,
_

ayanda83
20th April 2015, 19:29
Damn! this has proven to be more difficult than I thought. Three days later and I am still struggling with this problem.

wysota
20th April 2015, 19:48
What is so difficult about it?

ayanda83
20th April 2015, 20:15
Whats difficult about it is that I still cannot keep the child item within the bounds of the parent item (i.e. when I drag move the child item, I don't want it to go beyond the bounding rect of the parent item). I was convinced that the code I posted above will do the trick, but guess I was wrong because it doesn't work.

wysota
21st April 2015, 06:24
anda_skoa told you it wouldn't do the trick because you are mixing two coordinate spaces.


I want to limit the movement of the child item to the bounds of the parent item

Bounds of the parent item:

QRectF bounds = parent->boundingRect();
Limit movement of child to that rect:

if(!bounds.contains(pos())) { ... }

Riddle:
What coordinate space are we using here?
a) item
b) item's parent
c) scene