How to increment Image name?

181 Views Asked by At

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

});
2

There are 2 best solutions below

2
Marius ROBERT On

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

  • 0%10=0
  • 1%10=1
  • ...
  • 10%10=0
  • 11%10=1
  • ...
  • 12971982%10=2
0
Hamza On

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