How to create screenshot of QWidget?

avalagne picture avalagne · Apr 30, 2012 · Viewed 16.4k times · Source

I work at my homework in Qt Creator, where I paint to QWidget and I need to save some part of this QWdiget.

I tried to solve this problem:

 QPixmap pixmap;
 pixmap.copy(rectangle); // rectangle is part of QWidget, which I need to save
 pixmap.save("example.png");

Thank you for help.

Answer

Mat picture Mat · Apr 30, 2012

You can use QWidget::render for this. Assuming rectangle is a QRect:

QPixmap pixmap(rectangle->size()); 
widget->render(&pixmap, QPoint(), QRegion(rectangle));