How do I calculate and set the new position of a QGraphicsItem relative to the new parent origin?

Question

How do I calculate and set the new position of a QGraphicsItem relative to the new parent origin?

Background information

I am developing a resize function that produces a bounding box with a single anchor when I double-click on a given object (rectangle, ellipse, etc.). The user can change the size of the object by adjusting the position of the anchor.

What works

The user can double-click and resize the object the first time they try to resize.

Problem observations

With successive attempts (passes) to resize, the user sees the object "jumping" (ie, sudden resizing) immediately after double-clicking on the object and moving the anchor, everything is so small. The object will not resize to any position, but exactly to the position of the previous parent.

To the root cause

I've narrowed it down to the following statement (snippet from our overloaded mousemoveevent () function):

dynamic_cast<RIShape*>(_item)->scaleBy(myp, xscale, yscale);

      

Link to online posting describing a similar issue

As I looked around a bit, before posting this post I came across the following link: http://www.qtcentre.org/threads/22775-Qgraphicsitem-parent-child-paint-problem

From this post, I needed to calculate and set the new position of the QGraphicsItem (_item) relative to the new parent origin. Hence the question is above.

PS Here are additional code snippets if you're interested:

A way of creating a resizer (bounding box with binding) and binding to the object to be modified.

QGraphicsItem* item = itemAt( event->pos() );
RIShape*       shape= dynamic_cast<RIShape*>(item);
if( item && shape )
{
  if( !_selector )
  {
    _selector = new RISelector(item);
...

      

Constructor initialization method for 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)

      

+3


source to share





All Articles