Results 1 to 3 of 3

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

  1. #1
    Join Date
    Dec 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

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

    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)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to calculate and set the position of an item relative to a new parent's origi

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2014
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: How to calculate and set the position of an item relative to a new parent's origi

    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


Similar Threads

  1. Replies: 0
    Last Post: 14th September 2013, 10:14
  2. Replies: 4
    Last Post: 13th April 2010, 16:03
  3. how to find mouse position relative to a cell
    By winkle99 in forum Qt Programming
    Replies: 0
    Last Post: 30th December 2009, 21:32
  4. relative position
    By franco.amato in forum Newbie
    Replies: 2
    Last Post: 8th December 2009, 18:08
  5. Replies: 3
    Last Post: 14th April 2006, 11:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.