'How to Crop Image in a QGraphicsView in Qt C++?

I am a beginner in Qt and for my school project I have to develop an Image Editor. The last feature I'm missing for the Editor is cropping the Image using a QRubberBand, and then crop the selected area using a push button. The problem is that I haven't found useful Information on the internet so far. There are reports of subclassing the GraphicsView, but I don't know how to connect it to the UI (I'm really confused).

Thanks in advance!



Solution 1:[1]

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 !

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yoruk