'How to increment Image name?
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");
});
Solution 1:[1]
You can use the modulo operator (%), for example :
- 0%10=0
- 1%10=1
- ...
- 10%10=0
- 11%10=1
- ...
- 12971982%10=2
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marius ROBERT |
| Solution 2 | Hamza |
