I would like to resize the boundingRect()
of my QGraphicsItem
using the mouse.
To build this I found this topic.
So I managed to make it work for the right, the bottomright and the bottom of my boundingRect()
following the idea of this the topic.
But since the position of my item is defined with the top left of the boundingRect()
it is more complicated to modify the size of my item moving the edges linked to this position.
I tried with the top left but the bottom right is also moving. I would like to have the bottom right fixed to only change the size and not the whole position.
Here is a part of my code:
void Myclass::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(_resizemode) //if bottom selected
{
prepareGeometryChange();
_h = event->pos().y();
}
if(_resizemode2) //if bottom right selected
{
prepareGeometryChange();
_h = event->pos().y();
_w = event->pos().x();
}
if(_resizemode3) //if right selected
{
prepareGeometryChange();
_w = event->pos().x();
}
if(_resizemode4) //if top left selected HERE IS MY ISSUE
{
prepareGeometryChange();
setPos(pos().x()+ event->pos().x(), pos().y() + event->pos().y());
_h = _h - event->pos().y();
_w = _w - event->pos().x();
}
if(!_resizemode&&!_resizemode2&&!_resizemode3&&!_resizemode4)
{
update();
QGraphicsItem::mouseMoveEvent(event);
}
}
_h
and _w
or the height and the width implemented here:
QRectF Aabb::boundingRect() const
{
return QRectF( 0, 0, _w, _h);
}
Does someone know how to resize a QGraphicsItem
selecting with the mouse each corners of the boundingRect()
please?
the position of my item is defined with the top left of the boundingRect()
In my opinion, the easiest way is to change this so that the position is defined by the centre of the item. In order to do this, define the original bounding rect as top left (-w/2, -h/2) and bottom right (w/2,h/2) or if you're returning in the boundingRect function QRectF(-w/2, -h/2, w, h) for (x,y,w,h).
Then, create child 'tool' items at the corners, which define the area which you can click and drag the corners of the original item. You may want these just to appear if the item is selected.
In the moveEvent of the child item, update the bounding rect of the main item so that it moves with the child. When the mouse is released, re-calculate the boundingRect of the main item, so that its centre is still at (0,0).
This can be done quite easily if you map the top left and bottom right of the boundingRect and convert them to scene-coordinates, then using the width and height, re-set the item's local coordinates back so that it maintains (-w/2, -h/2, w, h);