How to calculate and set the new position of a QGraphicsItem relative to a new parent's origin?

479 views Asked by At

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 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 suddenly resize to any position, but 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: http://www.qtcentre.org/threads/22775-Qgraphicsitem-parent-child-paint-problem

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.

P.S. Here are additional code snippets, 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::DashLine, Qt::RoundCap, Qt::RoundJoin),
  _anchorPen(Qt::black, 2, Qt::SolidLine), _item(item), _isLef(false)
0

There are 0 answers