15

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.

avalagne
  • 359
  • 3
  • 7
  • 15

2 Answers2

25

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

QPixmap pixmap(rectangle->size()); 
widget->render(&pixmap, QPoint(), QRegion(rectangle));
Mat
  • 202,337
  • 40
  • 393
  • 406
8

From QWidget::Grab:

QPixmap QWidget::grab(const QRect &rectangle = QRect(QPoint(0, 0), QSize(-1, -1)))
iammilind
  • 68,093
  • 33
  • 169
  • 336
Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • Genius ! I'm making my widgets drag-n-dropable. And the mime data has an icon that you can set, which shows up under the cursor when you drag outside of the container. – MathCrackExchange Apr 28 '19 at 00:50