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. Can't save an image from a QLabel to disc
Forum Updated to NodeBB v4.3 + New Features

Can't save an image from a QLabel to disc

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.3k Views 1 Watching
  • 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.
  • G Offline
    G Offline
    georgiav
    wrote on last edited by
    #1

    Hi, I am trying to convert a QPixmap to a QImage from a QLabel named label2 , and then save the image .

    QImage image;
    QString imagePath = QFileDialog::getSaveFileName( this,tr("Save File"),"", tr("JPEG (*.jpg .jpeg);;PNG (.png)" ));

             QPixmap pixmap = ui->label2->pixmap();
            if (!pixmap.isNull() )
            {
                 image =pixmap.toImage() ;
            }
    
    image.save(imagePath);
    

    This is what I've tried but it's not working, it doesn't save the image. Do you know why?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @georgiav said in Can't save an image from a QLabel to disc:

      but it's not working, it doesn't save the image.

      Please be more specific - is a file generated? Is imagePath valid (with a proper extension)? Is pixmap not null? Simply add some debug output to see what exactly does not work.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Christian EhrlicherC 1 Reply Last reply
      0
      • G Offline
        G Offline
        georgiav
        wrote on last edited by
        #3

        I checked and the imagePath is right but it seems like pixmap is null and it shouldn't be. Is that the correct way to get a QPixmap from a QLabel?

        Christian EhrlicherC 1 Reply Last reply
        0
        • G georgiav

          I checked and the imagePath is right but it seems like pixmap is null and it shouldn't be. Is that the correct way to get a QPixmap from a QLabel?

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @georgiav said in Can't save an image from a QLabel to disc:

          way to get a QPixmap from a QLabel?

          When you set a pixmap on the label previously, yes.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          G 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @georgiav said in Can't save an image from a QLabel to disc:

            way to get a QPixmap from a QLabel?

            When you set a pixmap on the label previously, yes.

            G Offline
            G Offline
            georgiav
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            so I changed it to this:

                    if (!pm.isNull() && !imagePath.isEmpty() )
                    {    cout<<"rfsd";
                         image =pm.toImage() ;
                         image.save(imagePath);
                    }
            

            and it prints rfsd but doesn't save the image.

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @georgiav said in Can't save an image from a QLabel to disc:

              but it's not working, it doesn't save the image.

              Please be more specific - is a file generated? Is imagePath valid (with a proper extension)? Is pixmap not null? Simply add some debug output to see what exactly does not work.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in Can't save an image from a QLabel to disc:

              Is imagePath valid (with a proper extension)?

              do you have write access in the selected given directory?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • G Offline
                G Offline
                georgiav
                wrote on last edited by
                #7

                @Christian-Ehrlicher could you please tell me how can I check that?

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Go to that folder and try to create a file for example.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    georgiav
                    wrote on last edited by
                    #9

                    @SGaist I tried now by naming the file image.jpeg and it works but when I name it only "image" it doesn't work even if I get the auto JPEG extention . How can I make it work in the last case?

                    f543850f-0177-4c98-a373-fabe4d675058-image.png

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Either ensure your file name ends with the proper extension or add the format to use in the save call.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        georgiav
                        wrote on last edited by georgiav
                        #11

                        @SGaist Okay, and how can I update this to save the extension?

                        QString imagePath = QFileDialog::getSaveFileName( this,tr("Save File"),"", tr("JPEG (*.jpg .jpeg);;PNG (.png)" ));

                        if I'd like the user to be able to choose if the image will be JPEG or PNG

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Use the selectedFilter output parameter to get the information.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2

                          • Login

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