[SOLVED] Resizing a rotated QGraphicsRectItem



  • Hi,
    I have a QGraphicsRectItem in my scene with a resize item like this post http://www.qtcentre.org/threads/3865...raphicsitem#18.
    I can rotate and resize the item with mouse dragging.
    The problem I met is when I try to resize the rectangle after rotation, the drag direction is in scene coordinate but the resize direction is in the item coordinate.
    !http://i50.tinypic.com/2d7tsp5.png()!
    For example, if I drag the bottom edge in the blue arrow direction, the edge actually moves in the white arrow direction.

    Could anyone tell me how to make them consistant? (Dragging to white arrow direction and resizing to white arrow direction.)

    This is my resizing code in mouseMoveEvent(), the center of item is (0,0) in the item coordinate:
    @QPointF pos = event->scenePos()-scenePos();

    if(m_selectedItem == 0) // left
    rect.setLeft(rectItem, pos.x()/rectItem->scale());
    else if(m_selectedItem == 1) // bottom
    rect.setBottom(pos.y()/rectItem->scale());
    else if(m_selectedItem == 2) // right
    rect.setRight(pos.x()/rectItem->scale());
    else if(m_selectedItem == 3) // top
    rect.setTop(pos.y()/rectItem->scale());@
    I tried the function mapToItem() but it seems not work, neither.



  • Any idea please?



  • heya seishin....

    i am writting a code for resizing from bttom of rectangle ( from which you are also doing in above post)

    objects: a Rectangle and a selection Box.(according to image url)

    selection Box is lying over rectangle with resize handles on edges.

    resizing is the process in which mousepress and mousemove and then is last mouserelease.

    solution:

    on mousepress on any handle of selection box( i.e. over rectangle) lets store boundingRect of rectangle in m_rectBBox.

    @
    in mousemoveEvent( QGraphicsSceneMouseEvent * event)
    {
    QRectF rectT = m_rectBBox;

    /** we are resizing in item coordinates , we have
    converted event scenepos in items coordinates, so
    that we can work in one coordinate system **/

    QPointF ptMouseMoveInItemsCoord = mapFromScene(event->scenePos());

    /** from below case we are going to resize rectange in
    only bottom direction from bottom handle (i.e. a square) **/

    case (BottomDirection)
    {

    rectT.setBottom( ptMouseMoveInItemsCoord.y() );
    rectT = rectT.normalized();
    setRect(rectT);

    }

    /** from below case we are going to resize rectange in only
    bottom Right direction from bottomRight handle (i.e a circle on edge )**/

    case (BottomRightDirection)
    {

    rectT.setBottomRight( ptMouseMoveInItemsCoord );
    rectT = rectT.normalized();
    setRect(rectT);

    }

    }

    in mouseReleaseEvent( QGraphicsSceneMouseEvent * event)
    {

    // now move the rectangle's origin back to center

    }

    @

    *Note:: It will solve your problem , read normalized() , let me know if u didnt understand this code or need more help .

    thnaks
    Nikhil Kumar



  • Hi Nikhil. I figured out the solution few days ago, which is exactly what you wrote. Thanks a lot for pointing this out. Hope this will help others in the future since I tried to find this solution for weeks.



  • Hello Selshin,
    Im on the same problem to ratoate and resize QGraphicsRectItems.
    As long as I have not rotated the RectItem, it works well.
    As soon I had rotated it the item behaves strange while resizing.

    The solution above couldn't help because I'm a newcomer.
    Would it be possible to post your solution.
    Thank you very much in advance.
    scafast



  • Hi scfast, as I said my solution is the same as niks1989's.
    The key idea here is to use
    @ mapFromScene(event->scenePos());@
    which means you need to convert the points from the scene coordinate to the item's coordinate.

    [quote author="scafast" date="1372073683"]Hello Selshin,
    Im on the same problem to ratoate and resize QGraphicsRectItems.
    As long as I have not rotated the RectItem, it works well.
    As soon I had rotated it the item behaves strange while resizing.

    The solution above couldn't help because I'm a newcomer.
    Would it be possible to post your solution.
    Thank you very much in advance.
    scafast[/quote]



  • Hello Swlshln,
    Thank you for your fast answer.
    I tried things like that, but, as I'm new in this area, I had no success.
    I'm shure, I made soething wrong.
    Would it be possible to post your code. May it would help me a lot.
    Thank your very much in advance.
    Best regards
    scafast



  • Hi scfast,

    My code is almost the same as what niks1989 has posted. There's just some variable names and code structure differences, so I don't think I need to post the code again.

    If the code niks1989 posted does not work for you, I'm sure mine neither.
    The core of the code is just using item coordinate system as I pointed in my last post, and there's simple transforms left so I think you might do something wrong in the other place.
    Maybe you can post your code and see if we can help.

    For some other words, I don't suggest to do this without basic graphics knowledge.

    [quote author="scafast" date="1372085677"]Hello Swlshln,
    Thank you for your fast answer.
    I tried things like that, but, as I'm new in this area, I had no success.
    I'm shure, I made soething wrong.
    Would it be possible to post your code. May it would help me a lot.
    Thank your very much in advance.
    Best regards
    scafast[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.