Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved How to save the widget containing pixmap which keep changing at certain time interval, as an image file.

    General and Desktop
    qpixmap qwidget grab
    4
    8
    2901
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • NIXIN
      NIXIN last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators last edited by

        hi,

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

        image.open(QIODevice::WriteOnly);
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 2
        • NIXIN
          NIXIN last edited by

          tried, still not working...

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @NIXIN last edited by

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 2
            • V
              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!");
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply Reply Quote 2
              • NIXIN
                NIXIN last edited by

                Checked

                Qfile::open() returns true, and also

                QPixmap::save() returns true.

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

                J.Hilk 1 Reply Last reply Reply Quote 0
                • J.Hilk
                  J.Hilk Moderators @NIXIN last edited by

                  @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 :)

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply Reply Quote 1
                  • NIXIN
                    NIXIN last edited by

                    Yes, a file with size 0 kb is being created

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post