Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Name of a file
Qt 6.11 is out! See what's new in the release blog

Name of a file

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 1.2k Views 1 Watching
  • 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.
  • J jackfr

    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-2

    QString frontcamera=QDateTime::currentDateTime().toString(QString("'%1/frontcamera_'dd-MM-yyyy'"):

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

    @jackfr

    • If you want your number to appear at the end of the filename do not offer a "'%1/frontcamera_'dd-MM-yyyy'" where the %1 is clearly at the beginning.
    • Look at QString::arg() for %1 support.
    J 1 Reply Last reply
    0
    • JonBJ JonB

      @jackfr

      • If you want your number to appear at the end of the filename do not offer a "'%1/frontcamera_'dd-MM-yyyy'" where the %1 is clearly at the beginning.
      • Look at QString::arg() for %1 support.
      J Offline
      J Offline
      jackfr
      wrote on last edited by
      #3

      @JonB
      when i removed %1 nothing is saved

      JonBJ 1 Reply Last reply
      0
      • J jackfr

        @JonB
        when i removed %1 nothing is saved

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

        @jackfr

        • 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 has frontcamera_ plus something for the %1 anywhere in it. It will only understand dd-MM-yyyy alone in the format string. Build your other bits of the string --- the frontcamera_ and the sequence number substitution --- outside of the call to currentDateTime().toString(dd-MM-yyyy).
        J 1 Reply Last reply
        1
        • JonBJ JonB

          @jackfr

          • 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 has frontcamera_ plus something for the %1 anywhere in it. It will only understand dd-MM-yyyy alone in the format string. Build your other bits of the string --- the frontcamera_ and the sequence number substitution --- outside of the call to currentDateTime().toString(dd-MM-yyyy).
          J Offline
          J Offline
          jackfr
          wrote on last edited by
          #5

          @JonB
          in fact it will overwrite the file each time

          JonBJ 1 Reply Last reply
          0
          • J jackfr

            @JonB
            in fact it will overwrite the file each time

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

            @jackfr
            I really should not have to do this for you, but try:

            QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(sequenceNumber)
            
            1 Reply Last reply
            2
            • M Offline
              M Offline
              mchinand
              wrote on last edited by mchinand
              #7

              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'))

              J 1 Reply Last reply
              1
              • M mchinand

                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'))

                J Offline
                J Offline
                jackfr
                wrote on last edited by
                #8

                @mchinand @JonB how can i add the path please i tried this but it didn't work
                QString filenamerecord_CA= QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(compteur_rec).arg(dossier);

                jsulmJ JonBJ 2 Replies Last reply
                0
                • J jackfr

                  @mchinand @JonB how can i add the path please i tried this but it didn't work
                  QString filenamerecord_CA= QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(compteur_rec).arg(dossier);

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

                  @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).

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

                  J 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @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).

                    J Offline
                    J Offline
                    jackfr
                    wrote on last edited by
                    #10

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

                    JonBJ jsulmJ 3 Replies Last reply
                    0
                    • J jackfr

                      @mchinand @JonB how can i add the path please i tried this but it didn't work
                      QString filenamerecord_CA= QString("frontcamera_%1_%2").arg(QDateTime::currentDateTime().toString("dd-MM-yyyy")).arg(compteur_rec).arg(dossier);

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

                      @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.

                      1 Reply Last reply
                      1
                      • J jackfr

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

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

                        @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?

                        1 Reply Last reply
                        1
                        • J jackfr

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

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

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

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

                          1 Reply Last reply
                          0
                          • J jackfr

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

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

                            @jackfr
                            Do yourself one favour: when a line gets as long as this and hard to read, factor out the

                            QString 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.

                            J 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @jackfr
                              Do yourself one favour: when a line gets as long as this and hard to read, factor out the

                              QString 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.

                              J Offline
                              J Offline
                              jackfr
                              wrote on last edited by jackfr
                              #15

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

                              J JonBJ 2 Replies Last reply
                              0
                              • J jackfr

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

                                J Offline
                                J Offline
                                jackfr
                                wrote on last edited by
                                #16

                                @jackfr solution : QString filenamerecord_CA= QString("%1/Camera_Avant %2").arg(dossier).arg(nom_fichier);

                                1 Reply Last reply
                                0
                                • J jackfr

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

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

                                  @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....

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved