Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to save the widget containing pixmap which keep changing at certain time interval, as an image file.

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpixmapqwidgetgrab
8 Posts 4 Posters 3.8k Views
  • 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.
  • NIXINN Offline
    NIXINN Offline
    NIXIN
    wrote on last edited by
    #1

    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
    0
    • J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      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


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

      1 Reply Last reply
      2
      • NIXINN Offline
        NIXINN Offline
        NIXIN
        wrote on last edited by
        #3

        tried, still not working...

        jsulmJ 1 Reply Last reply
        0
        • NIXINN NIXIN

          tried, still not working...

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5
            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
            2
            • NIXINN Offline
              NIXINN Offline
              NIXIN
              wrote on last edited by
              #6

              Checked

              Qfile::open() returns true, and also

              QPixmap::save() returns true.

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

              J.HilkJ 1 Reply Last reply
              0
              • NIXINN NIXIN

                Checked

                Qfile::open() returns true, and also

                QPixmap::save() returns true.

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

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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


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

                1 Reply Last reply
                1
                • NIXINN Offline
                  NIXINN Offline
                  NIXIN
                  wrote on last edited by
                  #8

                  Yes, a file with size 0 kb is being created

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved