Name of a file
-
how to add counter in name please
it means when I record the first time the name is: frontcamera_16-05-2022-1
for the second time it will : frontcamera_16-05-2022-2QString frontcamera=QDateTime::currentDateTime().toString(QString("'%1/frontcamera_'dd-MM-yyyy'"):
-
- If you remove the
%1
completely how do you expect any sequence number to be injected into the string under any circumstances? I never suggested you do that. - You do not seem to have done anything at all about looking up "
QString::arg()
for%1
support". - I severely doubt that
QDateTime::toString()
will recognise anything about your format string which hasfrontcamera_
plus something for the%1
anywhere in it. It will only understanddd-MM-yyyy
alone in the format string. Build your other bits of the string --- thefrontcamera_
and the sequence number substitution --- outside of the call tocurrentDateTime().toString(dd-MM-yyyy)
.
- If you remove the
-
In addition to what @JonB wrote, for file sorting purposes, I would suggest reversing the date to be specified as "yyyy-MM-dd" and also to zero-pad your counter variable. The number of zeros to pad (see QString's arg docs) with will depend on how many images you expect to collect in a day (alternatively you could use the time from the QDateTime object). This will make your counter string have 5 characters using '0' to fill in characters that are not used for the number (e.g., '00001'):
QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")).arg(sequenceNumber, 5, 10, QChar('0'))
-
@jackfr You have two % in your string (%1 and %2), but three arg() - why? If you want to put 3 substrings you need also %3.
And if you say "didn't work" please also say what exactly sin't working (what you expect vs, what you get). -
@jsulm thank you for your reply
error : QString::arg: Argument missing: 12-05-2022, /home/pi/Desktop/Mission_12-05-2022
code :
QString filenamerecord= QString("frontcamera_%1_%2_%3").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy").arg(dossier)).arg(compteur_rec); -
@jackfr said in Name of a file:
QString filenamerecord_CA= QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(compteur_rec).arg(dossier);
I asked you to read the docs for QString QString::arg(), as well as showing you a working example for your original question. I do not think you would have written this if you had taken the time to do so. Please do a bit of reading yourself before just randomly trying things which don't work and then asking here for others to sort it out for you.
-
@jackfr said in Name of a file:
QString filenamerecord= QString("frontcamera_%1_%2_%3").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy").arg(dossier)).arg(compteur_rec);
FGS, did you even look at where your own
)
s are before posting this? -
@jackfr said in Name of a file:
QString filenamerecord= QString("frontcamera_%1_%2_%3").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy").arg(dossier)).arg(compteur_rec);
Please do what @JonB suggested: read documentation and try to understand what you are doing.
Here how it should look like:QString filenamerecord= QString("frontcamera_%1_%2_%3").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(dossier).arg(compteur_rec);
-
@jackfr
Do yourself one favour: when a line gets as long as this and hard to read, factor out theQString date = QDateTime::currentDateTime().toString("dd-MM-yyyy");
onto a line above your line and just use
date
in your line. It makes it easier to read, more manageable, less prone to parenthesis-errors. -
@JonB @jsulm
thank you for your answers in fact I read the documentation well and I made this code the problem is that I would like the file to be saved in the folder"dossier"
but i found the file is save in desktop with name "Mission_13-05-2022 Camera_Avant 2"
and not Camera_Avant 2 in the folder "dossier "
the code :compteur_rec =compteur_rec +1;
QString s = QString::number(compteur_rec);
QString nom_fichier= QString(s);
QString nom_fichier1= QString("Camera_Avant");
QString dossier = QDateTime::currentDateTime().toString(QString("'%1/Mission_'dd-MM-yyyy'").arg("/home/pi/Desktop"));
QDir().mkdir(dossier);//creation du dossier
QString filenamerecord=QString("%1 %2").arg(nom_fichier1).arg(nom_fichier);
QString filenamerecord_CA=QString("%1 %2").arg(filenamerecord).arg(dossier); -
@jackfr said in Name of a file:
QString dossier = QDateTime::currentDateTime().toString(QString("'%1/Mission_'dd-MM-yyyy'").arg("/home/pi/Desktop"));
This is quite wrong and makes no sense, as I previously stated in an earlier reply when you used it. Did you read that reply? Think about/look up in the docs what
QDateTime::toString()
accepts as a format string....