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. Rotating QRect using QTransform and Cropping a QImage with it
Forum Updated to NodeBB v4.3 + New Features

Rotating QRect using QTransform and Cropping a QImage with it

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.0k 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.
  • S Offline
    S Offline
    starkm42
    wrote on last edited by starkm42
    #1

    Hi,

    i am trying to achieve the following, having a QRect of some size, i rotate it at its center and crop a QImage like below:

    QRect cropRect(0, 0, height, width);
    QTransform transRect;
    QPoint centre = cropRect.Centre();
    transRect.translate(center.x(), center.y());
    tansRect.rotate(60.0);
    tansRect.translate(-centre.x(), -center.y());
    
    cropRect = transRect.mapRect(cropRect);
    
    QImage croppedImage = originalImage.copy(cropRect);
    

    well i figured out using the hard way that tansRect.rotate(60.0); does not exactly work like required here. it does rotate rectangle along center but the resulting rectangle is a bounding box of the rectangle that is rotated.

    one workaround was using a polygon

    QPolygon transformedPoly = transRect.mapToPolygon(cropRect);
    

    but all my attempts to get a QRect out of this polygon has gone in vain.

    a failed attempt

            cropRectangle.setTopLeft(transformedPoly.at(3));
            cropRectangle.setTopRight(transformedPoly.at(2));
            cropRectangle.setBottomRight(transformedPoly.at(1));
            cropRectangle.setBottomLeft(transformedPoly.at(0));
    

    any pointers are appreciated, thank !

    C 1 Reply Last reply
    0
    • S starkm42

      Hi,

      i am trying to achieve the following, having a QRect of some size, i rotate it at its center and crop a QImage like below:

      QRect cropRect(0, 0, height, width);
      QTransform transRect;
      QPoint centre = cropRect.Centre();
      transRect.translate(center.x(), center.y());
      tansRect.rotate(60.0);
      tansRect.translate(-centre.x(), -center.y());
      
      cropRect = transRect.mapRect(cropRect);
      
      QImage croppedImage = originalImage.copy(cropRect);
      

      well i figured out using the hard way that tansRect.rotate(60.0); does not exactly work like required here. it does rotate rectangle along center but the resulting rectangle is a bounding box of the rectangle that is rotated.

      one workaround was using a polygon

      QPolygon transformedPoly = transRect.mapToPolygon(cropRect);
      

      but all my attempts to get a QRect out of this polygon has gone in vain.

      a failed attempt

              cropRectangle.setTopLeft(transformedPoly.at(3));
              cropRectangle.setTopRight(transformedPoly.at(2));
              cropRectangle.setBottomRight(transformedPoly.at(1));
              cropRectangle.setBottomLeft(transformedPoly.at(0));
      

      any pointers are appreciated, thank !

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @starkm42

      What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.

      I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.

      BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.

      S 2 Replies Last reply
      2
      • C ChrisW67

        @starkm42

        What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.

        I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.

        BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.

        S Offline
        S Offline
        starkm42
        wrote on last edited by
        #3

        @ChrisW67

        A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.

        yes, i realized that.

        I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.

        well that could also be done i guess, let me check and get back to you.

        It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.

        i corrected it, please check the updated snippet.

        Thanks !

        1 Reply Last reply
        0
        • C ChrisW67

          @starkm42

          What are you expecting to get out of this process? A QImage is a rectangular area aligned with the axes, but you seem to be expecting something else.

          I think what you want to do is rotate the image 60 degrees, crop it using a rectangle, and then maybe rotate the result back.

          BTW: It would help if you posted (copy and paste) your actual code. That first snippet will not compile; there is no QRect::Centre() or variable tansrect.

          S Offline
          S Offline
          starkm42
          wrote on last edited by
          #4

          @ChrisW67 the actual code is not of a sample program and has un-necessary application logic which i feel is not required here. do let me know if there is anything else i can provide here.

          Thanks.

          C 1 Reply Last reply
          0
          • S starkm42

            @ChrisW67 the actual code is not of a sample program and has un-necessary application logic which i feel is not required here. do let me know if there is anything else i can provide here.

            Thanks.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @starkm42 said in Rotating QRect using QTransform and Cropping a QImage with it:

            if there is anything else i can provide here

            Yes. You can explain what you are expecting to achieve. What should the final image look like in relation to the original?

            S 1 Reply Last reply
            0
            • C ChrisW67

              @starkm42 said in Rotating QRect using QTransform and Cropping a QImage with it:

              if there is anything else i can provide here

              Yes. You can explain what you are expecting to achieve. What should the final image look like in relation to the original?

              S Offline
              S Offline
              starkm42
              wrote on last edited by
              #6

              @ChrisW67 need to crop a image from a large image, QImage.copy() accepts a QRectangle object (equivalent to cropping a section), now to get the rectangle we need to rotate one around the center at X, Y from origin where (X< OriginalImage.Width, Y < OriginalImage.Height and X + CropRectangle.Width() < OriginalImage.Width , Y + CropRectangle.Height < OriginalImage.Height).

              something like below
              Untitled Diagram.drawio.png

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                Here is an idea you can try: Instead of a QImage use QPixmap. You can use a QPainter to draw to a pixmap. Now draw the original image rotated in the opposite direction onto the pixmap. From this rotated pixmap you can now do your cropping with a non-rotated rectangle.

                S 1 Reply Last reply
                1
                • S SimonSchroeder

                  Here is an idea you can try: Instead of a QImage use QPixmap. You can use a QPainter to draw to a pixmap. Now draw the original image rotated in the opposite direction onto the pixmap. From this rotated pixmap you can now do your cropping with a non-rotated rectangle.

                  S Offline
                  S Offline
                  starkm42
                  wrote on last edited by
                  #8

                  @SimonSchroeder so rotating the painter itself and later cropping, yes i think it was suggested as well. and yes that seems to work for now. will be marking this solved.

                  1 Reply Last reply
                  0
                  • S starkm42 has marked this topic as solved on

                  • Login

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