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. Copy rotated part of QImage
Forum Update on Monday, May 27th 2025

Copy rotated part of QImage

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.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.
  • dream_captainD Offline
    dream_captainD Offline
    dream_captain
    wrote on last edited by
    #1

    Hello, i have a QGraphicsScene with a QGraphicsPixmapItem as a background and custom rotateble QGraphicsItem as a frame. I need to cut an area from the background and save it as a QImage. So far, i've tried to get mapToScene(boundingRect()) coordinates of QGraphicsItem and cut resulting Rect from QGraphicsPixmapItem->pixmap()->toImage(). It doesn't because QRect is wrong.
    alt text

    kshegunovK 1 Reply Last reply
    0
    • dream_captainD dream_captain

      Hello, i have a QGraphicsScene with a QGraphicsPixmapItem as a background and custom rotateble QGraphicsItem as a frame. I need to cut an area from the background and save it as a QImage. So far, i've tried to get mapToScene(boundingRect()) coordinates of QGraphicsItem and cut resulting Rect from QGraphicsPixmapItem->pixmap()->toImage(). It doesn't because QRect is wrong.
      alt text

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @dream_captain said in Copy rotated part of QImage:

      QRect is wrong

      It's not wrong per se, QRect is a normal rectangle, i.e. aligned with the axes, so it can't be used to represent a rotated rectangle. What you can do, is for example to cut the bounding rect, rotate the image to the proper angle, and then cut a second time. Finally rotate back to obtain the correct orientation. This however will not come without cost - when rotating raster data you will lose some fidelity in the process (as pixels are always aligned with the axes).
      Another possibility is to write your own copy() function that accepts a QPolygon, but this is going to require some more math to calculate which pixels fall into the polygon and which do not.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      dream_captainD 1 Reply Last reply
      2
      • kshegunovK kshegunov

        @dream_captain said in Copy rotated part of QImage:

        QRect is wrong

        It's not wrong per se, QRect is a normal rectangle, i.e. aligned with the axes, so it can't be used to represent a rotated rectangle. What you can do, is for example to cut the bounding rect, rotate the image to the proper angle, and then cut a second time. Finally rotate back to obtain the correct orientation. This however will not come without cost - when rotating raster data you will lose some fidelity in the process (as pixels are always aligned with the axes).
        Another possibility is to write your own copy() function that accepts a QPolygon, but this is going to require some more math to calculate which pixels fall into the polygon and which do not.

        Kind regards.

        dream_captainD Offline
        dream_captainD Offline
        dream_captain
        wrote on last edited by
        #3

        @kshegunov Thanks!
        But how i can move my cropping rectangle from one coordinate system to another? (cropped area will have it's own coordinate system).

        kshegunovK 1 Reply Last reply
        0
        • dream_captainD dream_captain

          @kshegunov Thanks!
          But how i can move my cropping rectangle from one coordinate system to another? (cropped area will have it's own coordinate system).

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @dream_captain said in Copy rotated part of QImage:

          But how i can move my cropping rectangle from one coordinate system to another?

          Well, my point was that you can't transform the rectangle itself, so you'd do something like this:

          QImage background;
          QTransform transform;
          QRect clipRectangle;
          
          QImage clippedImage = background.transformed(transform).copy(clipRectangle).transformed(transform.inverted());
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2

          • Login

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