PDA

View Full Version : How to calculate and set the position of an item relative to a new parent's origin?



CryuffFan
3rd December 2014, 21:22
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:

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.


Reference to Further Code Snippets that May Hold a Solution Key


https://qt.gitorious.org/qt/drdanz-qt/commit/615f8a53355ea501e489eecddab1d5c4295cab80
QTransform thisToParentTransform = transformToParent();
4284 QTransform thisToParentTransform = transformData
4285 ? transformData->computedFullTransform() * QTransform::fromTranslate(newPos.x(), newPos.y())
4286 : QTransform::fromTranslate(newPos.x(), newPos.y());


http://qt-project.org/doc/qt-4.8/qgraphicsitem.html
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::DashLine, Qt::RoundCap, Qt::RoundJoin),
_anchorPen(Qt::black, 2, Qt::SolidLine), _item(item), _isLef(false)

wysota
4th December 2014, 08:40
I have to say I like the way you asked your question giving much info on the problem. It is a pitty not many people do that.

Where exactly does a change of parent occur? I don't think resizing an item requires any such actions. By the way you are scaling and not resizing the items. Remember that scaling is done around the origin point, maybe that's one of your problems.

CryuffFan
5th December 2014, 15:45
Thanks a lot for getting back to me on this, wysota!


To answer your question

There is no change of parent. It is just that the parent of the object being scaled is not getting updated.


Key code snippet of interest

virtual void scaleBy(QPointF p, double xscale, double yscale)
{
QPointF myp = boundingRect().topLeft();
setTransform(QTransform().translate(myp.x(), myp.y())
.scale(xscale, yscale)
.translate(-myp.x(), -myp.y()));
}


Further analysis of the problem: Debug statements added

qDebug()<<"top left boundRect xB = "<< _item->boundingRect().topLeft().x() <<" top left boundRect yB = "<< _item->boundingRect().topLeft().y()
<<"height boundRect Bef = "<< _item->boundingRect().height() <<" width boundRect Bef = "<< _item->boundingRect().width();

qDebug()<<"top left sceneBoundRect xB = "<< _item->sceneBoundingRect().topLeft().x() <<" top left sceneBoundRect yB = "<< _item->sceneBoundingRect().topLeft().y()
<<"height sceneBoundRect Bef = "<< _item->sceneBoundingRect().height() <<" width sceneBoundRect Bef = "<< _item->sceneBoundingRect().width();

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

qDebug()<<"top left boundRect xB = "<< _item->boundingRect().topLeft().x() <<" top left boundRect yB = "<< _item->boundingRect().topLeft().y()
<<"height boundRect Bef = "<< _item->boundingRect().height() <<" width boundRect Bef = "<< _item->boundingRect().width();

qDebug()<<"top left sceneBoundRect xB = "<< _item->sceneBoundingRect().topLeft().x() <<" top left sceneBoundRect yB = "<< _item->sceneBoundingRect().topLeft().y()
<<"height sceneBoundRect Bef = "<< _item->sceneBoundingRect().height() <<" width sceneBoundRect Bef = "<< _item->sceneBoundingRect().width();
#endif


Further analysis of the problem: The corresponding debugging results

top left boundRect xB = -28 top left boundRect yB = 27 height boundRect Bef = 100 width boundRect Bef = 100

top left sceneBoundRect xB = -28 top left sceneBoundRect yB = 27 height sceneBoundRect Bef = 205 width sceneBoundRect Bef = 206

top left boundRect xB = -28 top left boundRect yB = 27 height boundRect Bef = 100 width boundRect Bef = 100

top left sceneBoundRect xB = -28 top left sceneBoundRect yB = 27 height sceneBoundRect Bef = 100 width sceneBoundRect Bef = 99.5146


2nd Question

Why does the sceneBoundingRect shrink from (h,w) = (205,206) to (h,w) = (100,99.5) upon calling the scaleBy() function?


Further Graphical Context


10784