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. Moving the rotated Rect back to original center after QPainter::rotate()
Qt 6.11 is out! See what's new in the release blog

Moving the rotated Rect back to original center after QPainter::rotate()

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 3.6k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Pauly
    wrote on last edited by
    #1
    qreal anyDegree=20;
    Rect myRect(0,0,100,100); //original center is (50, 50)
    QPainter painter;
    painter.drawRect(myRect);
    painter.rotate(anyDegree); //need to find the new center of this rotated shape?
    

    I'd like to move the rotated shape back to it's original location (50,50). I'm thinking to calculate the new center of rotated rect with QtMath cos and sin etc, but that seems pretty nasty... I wonder if there is a more elegant way to find the new center of a rotated Rect after QPainter::rotate()? Or is there a better way to move rect back after such rotation? Thank you very much.

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by m.sue
      #2

      Hi,
      do you mean you want to rotate around the original center? To rotate around (any) given point you just have to translate to that point, rotate there and then translate back again. Something like this:

      painter.translate(center);
      painter.rotate(anyDegree);
      painter.translate(-center);
      

      -Michael.

      P 1 Reply Last reply
      1
      • m.sueM m.sue

        Hi,
        do you mean you want to rotate around the original center? To rotate around (any) given point you just have to translate to that point, rotate there and then translate back again. Something like this:

        painter.translate(center);
        painter.rotate(anyDegree);
        painter.translate(-center);
        

        -Michael.

        P Offline
        P Offline
        Pauly
        wrote on last edited by
        #3

        @m.sue

        This is great. Translate it to (0,0) for the rotation then translate back to original center... smart!

        P 1 Reply Last reply
        0
        • P Pauly

          @m.sue

          This is great. Translate it to (0,0) for the rotation then translate back to original center... smart!

          P Offline
          P Offline
          Pauly
          wrote on last edited by
          #4

          @Pauly

          Actually this does not quite work if I have two shapes... What should I do if I just want to rotate one but not the other? It seems that painter.translate and rotate affects all shapes drawn on it... Thanks.

          FlotisableF 1 Reply Last reply
          0
          • P Pauly

            @Pauly

            Actually this does not quite work if I have two shapes... What should I do if I just want to rotate one but not the other? It seems that painter.translate and rotate affects all shapes drawn on it... Thanks.

            FlotisableF Offline
            FlotisableF Offline
            Flotisable
            wrote on last edited by
            #5

            @Pauly
            you can use save and restore

            take a look at analog clock example,it teach you how to use QPainter

            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