1

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!

nowsqwhat
  • 101
  • 7

1 Answers1

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 !

Yoruk
  • 103
  • 8
  • Thanks for your answer, but I want to select the Area using my Mouse with MouseEvents. And the Problem is that I want to Crop the Image on the start point (press) and end point (release), inside the GraphicsView – nowsqwhat May 16 '22 at 06:07
  • My answer is still correct, you will have to the copy() to get the cropped picture. But if you want to use the mouse to get the image corners (to create the "rect"), you can use an event filter to catch the mouse coordinates on your scene and the buttons status. https://doc.qt.io/qt-5/eventsandfilters.html – Yoruk May 16 '22 at 06:24
  • Ok I will try it out! – nowsqwhat May 16 '22 at 07:33