PDA

View Full Version : QGraphicsRectItem positioning problem



gufeatza
4th September 2009, 07:04
Hello, I come to you with a bothering problems I met.

I am trying to create and use an object derived from QGraphicsRectItem inside a QGraphicsWidget by the next method: I create it with the current widget as parent parameter, but in a dynamic way – as dragging the mouse throughout the scene, the object is expanding until the mouse is released(consecutive setRect methods applied to the item in mouseMoveEvent handle).

Afterward I attach the newly created object to the current scene.

My object overrides the method itemChange so when the change ItemPositionChange materializes, I do the classic object positioning for it to be limited in the scene's rect as it follows:

Quoted

QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPointF newPos = value.toPointF();
QRectF rect = scene()->sceneRect();
if (!rect.contains(newPos)) {
// Keep the item inside the scene rect.
newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
return newPos;
}
}
return QGraphicsItem::itemChange(change, value);
}



The problem is, as you guessed, that the method doesn't do it's job(it acts as if the scene's origin point, which is (0, 0) in the viewport, would be somewhere in the middle of that scene so the rect item's movement being limited by that point). So I tried to apply a mapToScene to that point, but seems that the returned value still isn't as it should be.

To be more clear, I set the item's position in the scene to the point (0, 0) and when ItemPositionChange shows up, it's value hits me with a point that does not map correctly to the scene.

So, let me know if anything is wrong in my thinking (God help), cause I believe if it's an anomaly I must override the method that handles the mouseMoveEvent to move the QGraphicsRectItem manually.

Thanks a lot!!

wysota
4th September 2009, 09:14
It would be nice if you provided a minimal compilable example reproducing the problem.

gufeatza
4th September 2009, 10:34
Unfortunately I tried to do this(reproduce the problem in a separate program) before posting, but with no success. And I cannot post the current application cause it's from work and I am not allowed to :(.
Also, I am aware of the fact that it might be a problem specific to my application.

Opened for any suggestion, meanwhile I started overriding mouseMoveEvent :(

wysota
5th September 2009, 00:24
Unfortunately I tried to do this(reproduce the problem in a separate program) before posting, but with no success.

Which probably means the problem is elsewhere. Try comparing the code you have written to reproduce the problem with your actual code and try to find the part that makes the difference. That's the main point of asking for a compilable example reproducing the problem - in many cases users notice the problem by themselves.