avalagne
avalagne

Reputation: 359

How to create screenshot of QWidget?

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.

Upvotes: 15

Views: 20996

Answers (2)

Valentin H
Valentin H

Reputation: 7458

From QWidget::Grab:

QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0), QSize(-1, -1)))

Upvotes: 8

Mat
Mat

Reputation: 206909

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

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

Upvotes: 25

Related Questions