Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to increment Image name ?
Qt 6.12 LTS is out! See what's new in the release blog

How to increment Image name ?

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 6.9k 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.
  • Cobra91151C Cobra91151

    @hamzaelazizi

    @KroMignon

    Your solution works well but counts from the second picture 2.png, since 1 + (id % 11), otherwise the second count would start from 0. So, I have improved it a bit by removing the 1 + from modulo operation and adding the check for < 1 .

    @jsulm

    Your solution using class member variable as identifier would also work nice.

    My code:

    QCamera *camera = new QCamera(this);
    QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);
    connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, QImage img) {
           qDebug() << "ID: " << id;
           int imgID = id % 11;
    
           if (imgID < 1) {
               imgID = 1;
           }
    
           QString path = QString("%1/%2.png").arg(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), QString::number(imgID));
           qDebug() << path;
           img.save(path, "PNG");
      });
    
    camera->setCaptureMode(QCamera::CaptureStillImage);
    camera->start();
    ui->pushButton_4->setText("Capture");
    connect(ui->pushButton_4, &QPushButton::clicked, [camera, imageCapture, this]() {
         camera->searchAndLock();
         imageCapture->capture();
         camera->unlock();
    });
    

    Result:

    ID:  1
    "C:/Users/cobra/Pictures/1.png"
    ID:  2
    "C:/Users/cobra/Pictures/2.png"
    ID:  3
    "C:/Users/cobra/Pictures/3.png"
    ID:  4
    "C:/Users/cobra/Pictures/4.png"
    ID:  5
    "C:/Users/cobra/Pictures/5.png"
    ID:  6
    "C:/Users/cobra/Pictures/6.png"
    ID:  7
    "C:/Users/cobra/Pictures/7.png"
    ID:  8
    "C:/Users/cobra/Pictures/8.png"
    ID:  9
    "C:/Users/cobra/Pictures/9.png"
    ID:  10
    "C:/Users/cobra/Pictures/10.png"
    ID:  11
    "C:/Users/cobra/Pictures/1.png"
    ID:  12
    "C:/Users/cobra/Pictures/1.png"
    ID:  13
    "C:/Users/cobra/Pictures/2.png"
    ID:  14
    "C:/Users/cobra/Pictures/3.png"
    ID:  15
    "C:/Users/cobra/Pictures/4.png"
    ID:  16
    "C:/Users/cobra/Pictures/5.png"
    ID:  17
    "C:/Users/cobra/Pictures/6.png"
    ID:  18
    "C:/Users/cobra/Pictures/7.png"
    ID:  19
    "C:/Users/cobra/Pictures/8.png"
    ID:  20
    "C:/Users/cobra/Pictures/9.png"
    ID:  21
    "C:/Users/cobra/Pictures/10.png"
    ID:  22
    "C:/Users/cobra/Pictures/1.png"
    ID:  23
    "C:/Users/cobra/Pictures/1.png"
    ID:  24
    "C:/Users/cobra/Pictures/2.png"
    ID:  25
    "C:/Users/cobra/Pictures/3.png"
    ID:  27
    "C:/Users/cobra/Pictures/5.png"
    ID:  29
    "C:/Users/cobra/Pictures/7.png"
    ID:  31
    "C:/Users/cobra/Pictures/9.png"
    ID:  32
    "C:/Users/cobra/Pictures/10.png"
    ID:  33
    "C:/Users/cobra/Pictures/1.png"
    

    Solution by class member variable:

    .h file

    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        Dialog(QWidget *parent = nullptr);
        ~Dialog();
    private:
        Ui::Dialog *ui;
        int imgID;
    };
    

    .cpp file

    Dialog::Dialog(QWidget *parent)
        : QDialog(parent), ui(new Ui::Dialog), imgID(0)
    {
        QCamera *camera = new QCamera;
        QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);
        connect(imageCapture, &QCameraImageCapture::imageCaptured, [=](int id, QImage img) {
           qDebug() << "ID: " << id;
           imgID++;
    
           if (imgID >= 11) {
               imgID = 1;
           }
    
           QString path = QString("%1/%2.png").arg(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), QString::number(imgID));
           qDebug() << path;
           img.save(path, "PNG");
        });
    
        camera->setCaptureMode(QCamera::CaptureStillImage);
        camera->start();
        ui->pushButton_4->setText("Capture");
        connect(ui->pushButton_4, &QPushButton::clicked, [camera, imageCapture, this]() {
            //on half pressed shutter button
            camera->searchAndLock();
            //on shutter button pressed
            imageCapture->capture();
            //on shutter button released
            camera->unlock();
        });
    }
    

    Output:

    ID:  1
    "C:/Users/cobra/Pictures/1.png"
    ID:  2
    "C:/Users/cobra/Pictures/2.png"
    ID:  3
    "C:/Users/cobra/Pictures/3.png"
    ID:  4
    "C:/Users/cobra/Pictures/4.png"
    ID:  5
    "C:/Users/cobra/Pictures/5.png"
    ID:  6
    "C:/Users/cobra/Pictures/6.png"
    ID:  7
    "C:/Users/cobra/Pictures/7.png"
    ID:  8
    "C:/Users/cobra/Pictures/8.png"
    ID:  9
    "C:/Users/cobra/Pictures/9.png"
    ID:  10
    "C:/Users/cobra/Pictures/10.png"
    ID:  11
    "C:/Users/cobra/Pictures/1.png"
    ID:  12
    "C:/Users/cobra/Pictures/2.png"
    ID:  13
    "C:/Users/cobra/Pictures/3.png"
    ID:  14
    "C:/Users/cobra/Pictures/4.png"
    ID:  15
    "C:/Users/cobra/Pictures/5.png"
    ID:  16
    "C:/Users/cobra/Pictures/6.png"
    ID:  18
    "C:/Users/cobra/Pictures/7.png"
    ID:  20
    "C:/Users/cobra/Pictures/8.png"
    ID:  21
    "C:/Users/cobra/Pictures/9.png"
    ID:  23
    "C:/Users/cobra/Pictures/10.png"
    ID:  25
    "C:/Users/cobra/Pictures/1.png"
    ID:  26
    "C:/Users/cobra/Pictures/2.png"
    ID:  28
    "C:/Users/cobra/Pictures/3.png"
    ID:  30
    "C:/Users/cobra/Pictures/4.png"
    ID:  31
    "C:/Users/cobra/Pictures/5.png"
    ID:  33
    "C:/Users/cobra/Pictures/6.png"
    ID:  35
    "C:/Users/cobra/Pictures/7.png"
    ID:  36
    "C:/Users/cobra/Pictures/8.png"
    ID:  37
    "C:/Users/cobra/Pictures/9.png"
    ID:  39
    "C:/Users/cobra/Pictures/10.png"
    ID:  41
    "C:/Users/cobra/Pictures/1.png"
    ID:  42
    "C:/Users/cobra/Pictures/2.png"
    ID:  43
    "C:/Users/cobra/Pictures/3.png"
    ID:  44
    "C:/Users/cobra/Pictures/4.png"
    ID:  45
    "C:/Users/cobra/Pictures/5.png"
    
    hamzaelaziziH Offline
    hamzaelaziziH Offline
    hamzaelazizi
    wrote on last edited by
    #7

    @Cobra91151 it didn't work, it gave me this error at first "error: assignment of read-only variable ‘imgID’
    imgID = id;"
    but when i took "imgID" inside the method, it kept only overwriting on image N°1.

    jsulmJ 1 Reply Last reply
    0
    • hamzaelaziziH hamzaelazizi

      @Cobra91151 it didn't work, it gave me this error at first "error: assignment of read-only variable ‘imgID’
      imgID = id;"
      but when i took "imgID" inside the method, it kept only overwriting on image N°1.

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

      @hamzaelazizi The method suggested by @Cobra91151 will not work as changing a parameter passed by value has NO effect on the original variable! Use your own id as I suggested above.

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

      1 Reply Last reply
      1
      • hamzaelaziziH hamzaelazizi

        Hello Everyone, as i was continuing to work on an application, i came across this issue an i still couldn't solve it for more than 4 days now. The issue is that i cannot increment the naming of the saved images to a certain number and then comeback to the number 0 and start overwriting those images until i reach than number again and so on repeatedly!
        let me give u an example : i want to save 10 Images and i want them to be names "1.png", "2.png"......"10.png", but i want the 11th one to overwrite the "1.png", 12th to overwrite the "2.png" and so on.

        i've tried multiple approaches but none of them worked! :(

        here's my code, thank you

        QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id, QImage img) {
           QString fileName = QString::number(id)+ ".png";
           QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
           img.save(path, "PNG");
         });
        
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by KroMignon
        #9

        @hamzaelazizi said in How to increment Image name ?:

        i've tried multiple approaches but none of them worked! :(

        I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?

        QString fileName = QString("%1.png").arg(1 + (id % 11));
        

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        jsulmJ hamzaelaziziH 2 Replies Last reply
        2
        • KroMignonK KroMignon

          @hamzaelazizi said in How to increment Image name ?:

          i've tried multiple approaches but none of them worked! :(

          I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?

          QString fileName = QString("%1.png").arg(1 + (id % 11));
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @KroMignon The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured

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

          KroMignonK 1 Reply Last reply
          0
          • jsulmJ jsulm

            @KroMignon The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #11

            @jsulm said in How to increment Image name ?:

            The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured

            I see! :D

            So this would also not work!
            Then I don't see another solution as what @jsulm already suggested: use class member to store the counter value

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            jsulmJ 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @hamzaelazizi said in How to increment Image name ?:

              i've tried multiple approaches but none of them worked! :(

              I don't know how you are generating the ID, if it is a free running counter why not use the modulo function?

              QString fileName = QString("%1.png").arg(1 + (id % 11));
              
              hamzaelaziziH Offline
              hamzaelaziziH Offline
              hamzaelazizi
              wrote on last edited by
              #12

              @KroMignon I wanted to use it, in fact i was trying it over and over but since i'm new to Qt i didn't know how to get it to work :( (i feel ashamed now), when i tried your solution, it seems like it's working! i don't know if i will be running into some issues in the futur, but i guess that would be a problem for another day, Thank you

              1 Reply Last reply
              0
              • KroMignonK KroMignon

                @jsulm said in How to increment Image name ?:

                The id is comming from https://doc.qt.io/qt-5/qcameraimagecapture.html#imageCaptured

                I see! :D

                So this would also not work!
                Then I don't see another solution as what @jsulm already suggested: use class member to store the counter value

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

                @KroMignon said in How to increment Image name ?:

                So this would also not work!

                Actually it should :-)

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

                KroMignonK 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @KroMignon said in How to increment Image name ?:

                  So this would also not work!

                  Actually it should :-)

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #14

                  @jsulm said in How to increment Image name ?:

                  Actually it should :-)

                  Okay, I never used QCameraImageCapture, so I supposed the ID would be the ID of the used camera and not if the image...
                  Maybe I should avoid the reply to posts for topics I not experimented in!

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  hamzaelaziziH 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @jsulm said in How to increment Image name ?:

                    Actually it should :-)

                    Okay, I never used QCameraImageCapture, so I supposed the ID would be the ID of the used camera and not if the image...
                    Maybe I should avoid the reply to posts for topics I not experimented in!

                    hamzaelaziziH Offline
                    hamzaelaziziH Offline
                    hamzaelazizi
                    wrote on last edited by
                    #15

                    @KroMignon Sir, your solution helped me, so i think you should keep relying to this kind of posts :D, i have one last question, @jsulm how can i use my own id variable ? should i create a variable inside this class ? i don't understand !

                    jsulmJ 1 Reply Last reply
                    0
                    • hamzaelaziziH hamzaelazizi

                      @KroMignon Sir, your solution helped me, so i think you should keep relying to this kind of posts :D, i have one last question, @jsulm how can i use my own id variable ? should i create a variable inside this class ? i don't understand !

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

                      @hamzaelazizi said in How to increment Image name ?:

                      should i create a variable inside this class ?

                      Yes and you initialize it with 0. Each time your slot is called you increment it and check whether it is bigger than the max id value you want to use, if it is you set it back to 0.

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

                      hamzaelaziziH 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @hamzaelazizi said in How to increment Image name ?:

                        should i create a variable inside this class ?

                        Yes and you initialize it with 0. Each time your slot is called you increment it and check whether it is bigger than the max id value you want to use, if it is you set it back to 0.

                        hamzaelaziziH Offline
                        hamzaelaziziH Offline
                        hamzaelazizi
                        wrote on last edited by
                        #17

                        @jsulm something like this ?

                        
                        QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) {
                         int idx = 0;
                         if(idx>=11)
                         {
                                 idx=0;
                         }
                         else
                         {
                                 idx++;
                         }
                          QString fileName = QString::number(idx) + ".png";
                          QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
                          img.save(path, "PNG");
                                    });
                        

                        Because i've tried this and it didn't work :/

                        jsulmJ Cobra91151C 2 Replies Last reply
                        0
                        • hamzaelaziziH hamzaelazizi

                          @jsulm something like this ?

                          
                          QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) {
                           int idx = 0;
                           if(idx>=11)
                           {
                                   idx=0;
                           }
                           else
                           {
                                   idx++;
                           }
                            QString fileName = QString::number(idx) + ".png";
                            QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
                            img.save(path, "PNG");
                                      });
                          

                          Because i've tried this and it didn't work :/

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

                          @hamzaelazizi said in How to increment Image name ?:

                          something like this ?

                          No.
                          In your code idx is a local variable and not a class member...

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

                          hamzaelaziziH 1 Reply Last reply
                          0
                          • hamzaelaziziH hamzaelazizi

                            @jsulm something like this ?

                            
                            QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) {
                             int idx = 0;
                             if(idx>=11)
                             {
                                     idx=0;
                             }
                             else
                             {
                                     idx++;
                             }
                              QString fileName = QString::number(idx) + ".png";
                              QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + "/" + fileName;
                              img.save(path, "PNG");
                                        });
                            

                            Because i've tried this and it didn't work :/

                            Cobra91151C Offline
                            Cobra91151C Offline
                            Cobra91151
                            wrote on last edited by
                            #19

                            @hamzaelazizi

                            I have updated my previous post. Please, try it again.

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @hamzaelazizi said in How to increment Image name ?:

                              something like this ?

                              No.
                              In your code idx is a local variable and not a class member...

                              hamzaelaziziH Offline
                              hamzaelaziziH Offline
                              hamzaelazizi
                              wrote on last edited by
                              #20

                              @jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only

                              JonBJ jsulmJ 2 Replies Last reply
                              0
                              • hamzaelaziziH hamzaelazizi

                                @jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #21

                                @hamzaelazizi Depends what you write and where.

                                1 Reply Last reply
                                0
                                • hamzaelaziziH hamzaelazizi

                                  @jsulm i see, but whenever i declare a variable outside the method i get this error saying that the variable i created is a Read-only

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

                                  @hamzaelazizi said in How to increment Image name ?:

                                  outside the method

                                  I never suggested to declare it outside of the method. I suggested to declare it as a class member:

                                  class YOUR_CLASS:
                                  {
                                  private:
                                      int idx{0};
                                  

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

                                  hamzaelaziziH 1 Reply Last reply
                                  2
                                  • jsulmJ jsulm

                                    @hamzaelazizi said in How to increment Image name ?:

                                    outside the method

                                    I never suggested to declare it outside of the method. I suggested to declare it as a class member:

                                    class YOUR_CLASS:
                                    {
                                    private:
                                        int idx{0};
                                    
                                    hamzaelaziziH Offline
                                    hamzaelaziziH Offline
                                    hamzaelazizi
                                    wrote on last edited by
                                    #23

                                    @jsulm i understand now, thank you for your explanation

                                    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
                                    • Unsolved