Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 802
avalagne Avatar asked Apr 30 '12 10:04

avalagne


People also ask

How do I take a screenshot of a website using jQuery?

HTML & Script – Include the jQuery library. Create screenshot() function where initialize html2canvas on the body. By default, html2canvas set the image background color to black if save the screenshot. With the use of background: '#fff' set background white.

What is QWidget?

The QWidget class is the base class of all user interface objects. The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order.


2 Answers

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

QPixmap pixmap(rectangle->size()); 
widget->render(&pixmap, QPoint(), QRegion(rectangle));
like image 77
Mat Avatar answered Sep 21 '22 04:09

Mat


From QWidget::Grab:

QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0), QSize(-1, -1)))
like image 41
Valentin H Avatar answered Sep 21 '22 04:09

Valentin H