2

I'm going to write a script in python 3 that will build some graph/diagram/chart in a window (or scroll area) (previously created in Qt Designer 5 and converted into *.py). Probably the graph will be bigger than screen size (like 2k x 3k pixels). How can I save a snapshot picture of that graph?

For example.

  1. User loads data and changes some settings.
  2. Python builds graph.
  3. User clicks "save snapshot".
  4. Python saves graphsnapshot.png.
  5. End.

UP1. Forgot to add. Graph is a code-generated combination of progressbars, labels, horizontal and vertical lines etc.

UP2. Proposed solution is for PyQt4. But I use PyQt5, where commands grabWidget and grabWindow are obsolete and aren't recommended to use.

Obsolete Members for QPixmap. The following members of class QPixmap are obsolete. They are provided to keep old source code working. We strongly advise against using them in new code.

Community
  • 1
  • 1
Devilhunter
  • 123
  • 2
  • 11
  • Forgot to add. Graph is a code-generated combination of progressbars, labels, horizontal and vertical lines etc. – Devilhunter Oct 09 '15 at 10:45
  • Possible duplicate of [Screenshot of a window using python](http://stackoverflow.com/questions/10705712/screenshot-of-a-window-using-python) – ekhumoro Oct 09 '15 at 18:24
  • [Obsolete Members for QPixmap](http://doc.qt.io/qt-5/qpixmap-obsolete.html) says " Use QWidget::grab() instead " – Mel Oct 12 '15 at 07:55
  • Solution found. Rodegast from python.su helped. – Devilhunter Nov 02 '15 at 09:03
  • 1
    You might want to review [Can I answer my own question?](http://stackoverflow.com/help/self-answer) and [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) - hint - it doesn't involve editing your question. – Damien_The_Unbeliever Nov 02 '15 at 09:10
  • tmoreau solution also works. Finally understood how to use it. – Devilhunter Nov 02 '15 at 09:11

1 Answers1

3

Ok. here is proper solution posting.

In order to save snapshot of a huge window (that can even be bigger than screen resolution) one can use some codes from below:

Way 1.

pix = QtGui.QPixmap(widget.size())
widget.render(pix)
pix.save("save.png")

Way 2.

pix=widget.grab()
pix.save("save.png")
Devilhunter
  • 123
  • 2
  • 11