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. Creating and using the very same directory for next savings
QtWS25 Last Chance

Creating and using the very same directory for next savings

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 2.7k Views
  • 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.
  • P Offline
    P Offline
    phil63
    wrote on last edited by
    #1

    Hi,

    Please find below what I am working on.
    As you can see I am creationg a subdirectory for each entry.
    I would like to create a subdirectory onlyy at the first entry and then save the other files (nom2,nom3,..) in this directory created in first place
    . Thank you for your help

    @
    fenetre::fenetre( QLineEdit *line,QLineEdit *line2)
    {
    QPushButton *quitter= new QPushButton("Quitter", this);
    quitter->move(100,200);
    connect(quitter,SIGNAL(clicked()),qApp,SLOT(quit()));

    QPushButton *next= new QPushButton("Next", this);
    next->move(100,150);
    connect(next,SIGNAL(clicked()),this,SLOT(monslot()));

    line= new QLineEdit(this); // un numéro
    line->move(10,50);

    line2= new QLineEdit(this);
    line2->move(10,80);
    }
    //--------------------------------------------------------
    fenetre::fenetre( )
    {
    QPushButton *quitter= new QPushButton("Quitter", this);
    quitter->move(100,200);
    connect(quitter,SIGNAL(clicked()),qApp,SLOT(quit()));

    QPushButton *next= new QPushButton("Next", this);
    next->move(100,150);
    connect(next,SIGNAL(clicked()),this,SLOT(monslot()));

    QLabel *NumClient= new QLabel(this);
    NumClient->setText("Client Numéro (1-99) : ");
    NumClient->move(150,50);

    line= new QLineEdit(this);
    line->move(10,50);

    line2= new QLineEdit(this);
    line2->move(10,80);
    }
    //---------------------------------------------
    fenetre::~fenetre()
    {
    delete ui;
    }
    //---------------------------------
    void fenetre::monslot()
    {
    this->hide();
    //----------------------
    int n=1;
    QDir lDir;
    QString dossier="repertoire";
    //------------------------------
    while (true){
    dossier = "repertoire"+ qApp->tr("-%1").arg(n);
    if ( !lDir.exists ( dossier ))
    {
    lDir.mkdir(dossier);
    break;
    }
    else
    n += 1;
    }
    //--------------------------------------
    QString DirectoryDeTravail=dossier+"\";
    //-----------------------------------
    QString f;
    f=qApp->tr("nom%1.txt").arg(line->text());
    QFile output(DirectoryDeTravail + f);
    //------------------------------------------------------
    bool ok = output.open(QFile::WriteOnly|QFile::Text);
    if (ok)
    {
    output.write(("Numero client = "));
    output.write((line->text() + "\n").toUtf8());
    output.write((line2->text() + "\n").toUtf8());
    output.close();
    }
    else
    {
    qDebug("Impossible d'ouvrir le fichier txt");
    }
    //--------------------------------------
    fenetre *h= new fenetre;
    h->setGeometry(100,100,300,300);
    h->show();
    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      At line 52, you have a forever loop that will create a new directory each time you call monslot since if the directory already exists you create a new one.

      I would also recommend you to take a look at the layout related class to setup your widget rather that position every piece by hand.

      Last point, please have a look at the Qt examples, you seem to have a mix of widgets created by hand, a ui built with designer, and you first constructor make me think you are not fully aware of what is going on.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phil63
        wrote on last edited by
        #3

        Hi,
        Thank you for your reply.
        You are right I am not always fully aware of what I am doing. But I try to improve my level of expertise day after day.

        And indeed in line 52 I have a forever-working loop.
        And that is in fact the origine of my question.

        I supposed I have to add a test such as:
        @
        if (first_entry) { //created the subdirectory}
        // then create the file and open it.
        @
        But I do not how to do it.
        Thank you for your help

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Could you describe your workflow ?
          i.e. :

          • user write something here
          • user click there
          • program should create thing here

          etc...

          That way it will be easier to come up with the right code

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phil63
            wrote on last edited by
            #5

            Hi,
            Ok
            User writes a number in lineEdit line and the corresponding company name in lineEdit line2.
            Then user presses the button 'next'
            the computer is supposed to create a directory named 'repertoire-1 ou 'repertoire-2 ou repertoire-3 ... according to what already exists.
            Then in this directory (for example repertoire-3, assumming that repertoire-1 and repertoire-2 already exist) the computer is supposed to save the several files in this directory (each time the user hits the button 'next').
            The files are named 'nom1, nom2,.... and each of these files contains line.text() (the client number) and line2.text(). (the company name)
            quit button is to exit the button.
            Thank you for your help

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              In that case:
              When do you really need to increment repertoire ? On the application start ? The first time the next button is pressed after the application has been started ?

              Also, why do you store these information in files ? Wouldn't a database be more suited ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • P Offline
                P Offline
                phil63
                wrote on last edited by
                #7

                Hi,
                Thank you for your reply.

                The directory named repertoire has to be incremented or created at the very first beginning. It could be done at the application start or when the next button is hit for the first time.

                Indeed these informations could be saved in a database. But the files would bring more flexibility in their future use. And moreover I would be happy to learn a bit more of programming in overcoming this difficulty.
                Thank you for your help

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then:

                  Add a boolean variable (member of your class) that you check when you hit your next button i.e.:
                  @
                  if (!_firstClick) {
                  _repertoirePath = createNewRepertoire();
                  _firstClick = true;
                  }

                  QFile nameFile(QString("%1/nom%2.txt").arg(_repertoirePath).arg(_nextFileNumber);

                  // Take a look at QTextStream to write your file

                  ++_nextFileNumber;
                  @

                  I encourage you think carefully about flexibility and future use. Having lots of numbered files in numbered directories might not be the best solution.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    phil63
                    wrote on last edited by
                    #9

                    Hi,

                    Then I have to write:
                    @
                    fenetre::fenetre( bool firstClick,QLineEdit *line,QLineEdit *line2){
                    //initialise firstClick to false
                    firstClick=false;
                    @

                    And inside fenetre::monSlot() , I have to insert the lines you wrote

                    Is that correct?
                    Thank you

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      No,

                      This fenetre constructor and how you use it is wrong.

                      You already have line and line2 as class member (otherwise your code would not compile)

                      Add firstClick after the declaration of line and line2 and please, get a good C++ book, that will help you a lot.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        phil63
                        wrote on last edited by
                        #11

                        Thank you for your help.
                        But it seems not to work. I certainly missed something.
                        When _firstClick is true , _repertoirePath=""

                        @
                        //In fenetre.h
                        public:
                        bool _firstClick;
                        @

                        @
                        // in main
                        fenetre Mafenetre;
                        Mafenetre._fitstClick=false;
                        Mafenetre.show();
                        @
                        @
                        void fenetre::monslot(){
                        .this->hide();
                        if (!_firstClick){
                        int n=1;
                        QDir lDir;
                        QString dossier="repertoire";
                        while (true){
                        dossier = "repertoire"+ qApp->tr("-%1").arg(n);
                        if ( !lDir.exists ( dossier )){
                        lDir.mkdir(dossier);
                        .break;
                        }
                        else
                        n += 1;
                        }
                        QString _repertoirePath=dossier+"\";
                        _firstClick=true;
                        }
                        .//-----------------------------------
                        QFile output(QString("%1/nom%2.txt").arg(_repertoirePath).arg(line.text());
                        //------------------------------------------------------
                        bool ok = output.open(QFile::WriteOnly|QFile::Text);
                        if (ok)
                        {..........}
                        @

                        1 Reply Last reply
                        0

                        • Login

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