Home > Back-end >  How to increment Image name?
How to increment Image name?

Time:05-02

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

});

CodePudding user response:

You can use the modulo operator (%), for example :

  • 0=0
  • 1=1
  • ...
  • 10=0
  • 11=1
  • ...
  • 12971982=2

CodePudding user response:

Here's the solution i've found :

QObject::connect(_image_capture.data(), &QCameraImageCapture::imageCaptured, [=] (int id=0, QImage img) {
        QString fileName = QString("%1.png").arg(1   (id % 11));
        QString path = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)   "/"   fileName;
        img.save(path, "PNG");
        });

Thank you for your help and time :D

  • Related