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!!
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!!