Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Resizing a rotated QGraphicsRectItem
QtWS25 Last Chance

[SOLVED] Resizing a rotated QGraphicsRectItem

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seishin
    wrote on 16 Nov 2012, 15:14 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seishin
      wrote on 19 Nov 2012, 15:03 last edited by
      #2

      Any idea please?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        niks1989
        wrote on 23 Nov 2012, 04:58 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seishin
          wrote on 23 Nov 2012, 15:11 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adolf_schmitter
            wrote on 24 Jun 2013, 11:34 last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seishin
              wrote on 24 Jun 2013, 14:33 last edited by
              #6

              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]

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adolf_schmitter
                wrote on 24 Jun 2013, 14:54 last edited by
                #7

                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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Seishin
                  wrote on 24 Jun 2013, 16:38 last edited by
                  #8

                  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]

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved