Question
How to calculate and set the new position of a QGraphicsItem relative to a new parent's origin?
Background Information
I am developing a resizing feature that brings up a bounding box with one anchor upon double-clicking on a given object (rectangle, ellipse, etc.). The user can resize the object by adjusting the position of the anchor.
What is Working
The user can double click on the object and resize fine, on the first resizing attempt.
Problematic Observations
On consecutive resizing attempts (passes), the user sees the object "jumping" (i.e., suddenly resizing) immediately after double-clicking on the object and moving the anchor ever so slightly. The object does not magically resize to some unknown position. It resizes exactly to the position of the previous parent.
Towards a Root Cause
I have narrowed down the problem to the following statement (snippet from our overloaded mousemoveevent() function):
dynamic_cast<RIShape*>(_item)->scaleBy(myp, xscale, yscale);
Reference to an Online Posting Describing a Similar Problem
As I looked around a little bit, prior to posting this message, I came across the following reference:
Judging from this message, I needed to calculate and set the new position of the QGraphicsItem (_item) relative to a new parent's origin. Hence, the question above.
Reference to Further Code Snippets that May Hold a Solution Key
QTransform thisToParentTransform = transformToParent();
4284 QTransform thisToParentTransform = transformData
4285 ? transformData->computedFullTransform() * QTransform::fromTranslate(newPos.x(), newPos.y())
4286 : QTransform::fromTranslate(newPos.x(), newPos.y());
QGraphicsItem::ItemParentChange 5 The item's parent changes. The value argument is the new parent item (i.e., a QGraphicsItem pointer). Do not callsetParentItem() in itemChange() as this notification is delivered; instead, you can return the new parent fromitemChange().
QGraphicsItem::ItemParentHasChanged 15 The item's parent has changed. The value argument is the new parent (i.e., a pointer to a QGraphicsItem). Do not callsetParentItem() in itemChange() as this notification is delivered. The return value is ignored.
Additional snippets from my code
Here are additional snippets from my code, in case you are interested:
Method of Creating the Resizer (Bounding Box with an Anchor) and Tying to the Object to be Resized
QGraphicsItem* item = itemAt( event->pos() );
RIShape* shape= dynamic_cast<RIShape*>(item);
if( item && shape )
{
if( !_selector )
{
_selector = new RISelector(item);
...
Method of Initializing the Constructor for the Resizer (RISelector)
RISelector(QGraphicsItem* item)
: QGraphicsRectItem(item->sceneBoundingRect()), _W(10.0),
_rect(item->sceneBoundingRect()), _pivot(0,0), _pressed(false), _isResizing(false),
_bndBoxPen(Qt::black, 2, Qt:ashLine, Qt::RoundCap, Qt::RoundJoin),
_anchorPen(Qt::black, 2, Qt::SolidLine), _item(item), _isLef(false)
Bookmarks