How to crop an image in a QGraphicsView?

348 views Asked by At

I have to develop an image editor. The last feature I'm missing for it is cropping the image using a QRubberBand, and then crop the selected area using a push button.

There are reports of subclassing QGraphicsView, but I don't know how to connect it to the UI.

1

There are 1 answers

2
Yoruk On

First you have to build a QRect with the area of the picture that you want to keep.

Then you can use the copy method on QImage to create a new picture containing only the rectangle area.

QRect rect(10, 10, 30, 30);  //X Y top left corner coordinates ,  width / height of the rectangle

QImage croppedImage = initialImage.copy(rect);

Next use a QGraphicsPixmapItem to add your picture in the scene :

  QGraphicsPixmapItem *unitaire = new QGraphicsPixmapItem();
   unitaire ->setPixmap(mySprite);
  m_scene->addItem(unitaire );

Good luck !