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....

    QPrinter *printer;
    QPainter painter(printer);

    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) ));

    QFile image(myfile.png);

    pixmap.save(&image, "PNG");

    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.



  • hi,

    first glance I would say, you're missing a line here:

    image.open(QIODevice::WriteOnly);
    


  • tried, still not working...


  • Moderators

    @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?



  • QPixmap pixmap = widget->grab(QRect(QPoint(0, 0), QSize( widgetMaxWidth, widgetMaxHeight) ));
    if(!pixmap.toImage().save("myfile.png"))
    qCritical("Save Failed!");
    


  • Checked

    Qfile::open() returns true, and also

    QPixmap::save() returns true.

    still the I am not getting the image file....



  • @NIXIN
    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.