How can I crop an image in Qt?

sashoalm picture sashoalm · Aug 10, 2011 · Viewed 30.8k times · Source

I load a PNG image in a QPixmap/QImage and I want to crop it. Is there a function that does that in Qt, or how should I do it otherwise?

Answer

hmuelner picture hmuelner · Aug 10, 2011

You can use QPixmap::copy:

QRect rect(10, 20, 30, 40);
QPixmap original('image.png');
QPixmap cropped = original.copy(rect);

There is also QImage::copy:

QRect rect(10, 20, 30, 40);
QImage original('image.png');
QImage cropped = original.copy(rect);