How to save the widget containing pixmap which keep changing at certain time interval, as an image file.
I am having a QWidget which conatins a QLabel. I have set a pixmap on this QLabel using setPixmap(). The pixmap is changing every 500 msec.
Now I want to print this widget as well as save it as an image file.
In order to do so I am using grab() and passing it to printer something like this....
QPixmap pixmap = widget->grab(QRect(QPoint(10,10, ), QSize( widgetMaxWidth, widgetMaxHeight) ));
painter.drawPixmap(0, 0, pixmap);
This works fine for me because I am able to see the drawing inside QPrintPreviewDialog.
Whereas, when I try to save it as an image by the same way;
QPixmap pixmap = widget->grab(QRect(QPoint(0, 0, ), QSize( widgetMaxWidth, widgetMaxHeight) ));
I dont get the image file.
I would like to know how to achieve it??
And Also, when I am getting the grabbed widget painted on printer, then why not as an image file, when I save the same grabbed widget.
first glance I would say, you're missing a line here:
tried, still not working...
@NIXIN "still not working" - is not really helpful. Your code does not contain ANY error handling!
Did you check whether the file was opened? QFile::open returns a boolean signalling whether the file was opened or not. Use http://doc.qt.io/qt-5/qiodevice.html#errorString to print the error in case it returns false.
Same for QPixmap::save returns a boolean, did you check it?
What is the path to the image file?
VRonin last edited by VRonin
QPixmap pixmap = widget->grab(QRect(QPoint(0, 0), QSize( widgetMaxWidth, widgetMaxHeight) )); if(!pixmap.toImage().save("myfile.png")) qCritical("Save Failed!");
Qfile::open() returns true, and also
QPixmap::save() returns true.
still the I am not getting the image file....
if file.open() returns true, you should at least have the file (with size 0) created in your destination.
Can you comfirm that?
If not, try to give it a specific path, not only the name+ ending, so you can at least be sure where to look for the file :)
Yes, a file with size 0 kb is being created