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. Qt creator Update text file with input data
Qt 6.11 is out! See what's new in the release blog

Qt creator Update text file with input data

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 3.0k 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.
  • R Offline
    R Offline
    Ruben12313
    wrote on last edited by Ruben12313
    #1

    Hello all so I have a text file that I can upload to a listWidget along that Im able to add an item and delete an item. My problem is to have the selected row be updated with extra string data, say I click on mayo and i want to add 10 (mayo 10) and update it to my text file.

    Screenshot 2024-07-25 104756.png

    void all::on_pushButton_clicked()
    {
    
    //update listwidget
        QFile file3("C:\\Users\\morta\\QT Projects\\add_delete\\prac.txt");
        file3.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
        QTextStream in3(&file3);
    
        QString s;
    
        //Detele item
        foreach (QListWidgetItem *NAME, ui->listWidget->selectedItems())
        {
             ui->listWidget->takeItem(ui->listWidget->row(NAME)); //grabbed the line of text i need
            QString line2 = ui->extraline->text();   //the number of items text
            qDebug()<<line2;
        }
    
        //Update the text file
        auto numRows = ui->listWidget->count();
        for(int row = 0; row < numRows; ++row)
        {
            auto item = ui->listWidget->item(row);
            s = item->text();
            file3.resize(0);   // Deletes all data from the textfile
            in3<<s;            //adds row to text file
            in3<<"\n";       //adds a line break
        }
        file3.close();
    }
    
    JonBJ 1 Reply Last reply
    0
    • R Ruben12313

      Hello all so I have a text file that I can upload to a listWidget along that Im able to add an item and delete an item. My problem is to have the selected row be updated with extra string data, say I click on mayo and i want to add 10 (mayo 10) and update it to my text file.

      Screenshot 2024-07-25 104756.png

      void all::on_pushButton_clicked()
      {
      
      //update listwidget
          QFile file3("C:\\Users\\morta\\QT Projects\\add_delete\\prac.txt");
          file3.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
          QTextStream in3(&file3);
      
          QString s;
      
          //Detele item
          foreach (QListWidgetItem *NAME, ui->listWidget->selectedItems())
          {
               ui->listWidget->takeItem(ui->listWidget->row(NAME)); //grabbed the line of text i need
              QString line2 = ui->extraline->text();   //the number of items text
              qDebug()<<line2;
          }
      
          //Update the text file
          auto numRows = ui->listWidget->count();
          for(int row = 0; row < numRows; ++row)
          {
              auto item = ui->listWidget->item(row);
              s = item->text();
              file3.resize(0);   // Deletes all data from the textfile
              in3<<s;            //adds row to text file
              in3<<"\n";       //adds a line break
          }
          file3.close();
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ruben12313
      QListWidget::editItem(QListWidgetItem *item) is a call to edit (the text of) an item. To edit an item "in place" you need to set its flags to include ItemIsEditable.There's an example in Python at https://stackoverflow.com/questions/47962839/how-to-make-qlistwidget-items-editable, doubtless there are others in C++, like old forum post here https://forum.qt.io/topic/8928/solved-making-qlistwidget-item-as-editable-from-qlistwidget-context-menu.

      Whether you add, delete or modify, to update the text file you need to rewrite the whole thing, whenever you wish to do so. You need to get rid of whatever you are trying to do in your code with resize(). You need to close your file after reading it in, and open (with truncate/overwrite) any time you want to write all the current data out (and of course close it when done writing it). That is how text files are updated.

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Ruben12313
        wrote on last edited by Ruben12313
        #3

        @JonB
        I tried: ui->listWidget->setFlags(Qt::ItemFlag::ItemIsEnabled); but Class QListWidgets has no setFlags in it.

        JonBJ 1 Reply Last reply
        0
        • R Ruben12313

          @JonB
          I tried: ui->listWidget->setFlags(Qt::ItemFlag::ItemIsEnabled); but Class QListWidgets has no setFlags in it.

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

          @Ruben12313
          Why did you try ItemIsEnabled when you need ItemIsEditable?
          Per the documentation, which you need to consult, and the example you set flags on individual items via void QListWidgetItem::setFlags(Qt::ItemFlags flags).

          R 1 Reply Last reply
          1
          • JonBJ JonB

            @Ruben12313
            Why did you try ItemIsEnabled when you need ItemIsEditable?
            Per the documentation, which you need to consult, and the example you set flags on individual items via void QListWidgetItem::setFlags(Qt::ItemFlags flags).

            R Offline
            R Offline
            Ruben12313
            wrote on last edited by
            #5

            @JonB yes sorry i was in a rush to head to my class but i tried both edit and enable but eitherway listWidgets does not have a setFlags

            JonBJ 1 Reply Last reply
            0
            • R Ruben12313

              @JonB yes sorry i was in a rush to head to my class but i tried both edit and enable but eitherway listWidgets does not have a setFlags

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

              @Ruben12313 I have answered and given you the link. You set flags on the items in the list widget, not the list widget itself.

              1 Reply Last reply
              1
              • R Offline
                R Offline
                Ruben12313
                wrote on last edited by
                #7

                @JonB , thanks for helping i had modified the code and i can edit but its not what im trying for, i forgot to show on my end that i have a line editbox where i need to type a number and after pressing the button it will add on to the selected item

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Ruben12313
                  wrote on last edited by
                  #8

                  solved it myself:
                  foreach (QListWidgetItem *NAME, ui->listWidget->selectedItems())
                  {
                  delete ui->listWidget->takeItem(ui->listWidget->row(NAME));
                  QStringList stringList;
                  stringList << NAME->text()+"\t"+line2;
                  ui->listWidget->addItems(stringList);
                  }

                  //Update the text file
                  auto numRows = ui->listWidget->count();
                  for(int row = 0; row < numRows; ++row)
                  {
                  auto item = ui->listWidget->item(row);
                  s = item->text();
                  file3.resize(0); // Delets all data from the textfile
                  in3<<s; //adds row to text file
                  in3<<"\n"; //adds a line break
                  }
                  file3.close();

                  JonBJ 1 Reply Last reply
                  0
                  • R Ruben12313 has marked this topic as solved on
                  • R Ruben12313

                    solved it myself:
                    foreach (QListWidgetItem *NAME, ui->listWidget->selectedItems())
                    {
                    delete ui->listWidget->takeItem(ui->listWidget->row(NAME));
                    QStringList stringList;
                    stringList << NAME->text()+"\t"+line2;
                    ui->listWidget->addItems(stringList);
                    }

                    //Update the text file
                    auto numRows = ui->listWidget->count();
                    for(int row = 0; row < numRows; ++row)
                    {
                    auto item = ui->listWidget->item(row);
                    s = item->text();
                    file3.resize(0); // Delets all data from the textfile
                    in3<<s; //adds row to text file
                    in3<<"\n"; //adds a line break
                    }
                    file3.close();

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

                    @Ruben12313
                    I'd love to know how your code correctly writes all the lines/rows to a file, considering the placement of your file3.resize(0); // Delets all data from the textfile.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Ruben12313
                      wrote on last edited by Ruben12313
                      #10

                      sure so in my header i created a pointer (int *amount).

                      on my code i used:

                      void MainWindow::on_pushButton_2_clicked()
                      {
                      //update listwidget
                              QFile file3("prac3.txt");
                              file3.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
                              QTextStream in3(&file3);
                      
                              QString line1 = ui->comboBox->currentText();
                              QString amount = ui->lineEdit->text();   //number of item text
                      //detele old item data
                              QString search(line1);
                              QList<QListWidgetItem *> list = ui->listWidget->findItems(line1, Qt::MatchContains);
                              for ( QListWidgetItem *item : list )
                              delete item;
                      //add updated item with amount
                              QStringList stringList;
                              stringList << line1+"\t"+amount;
                              ui->listWidget->addItems(stringList);
                      //Update the text file
                              auto numRows = ui->listWidget->count();
                              for(int row = 0; row < numRows; ++row)
                              {
                                  auto item = ui->listWidget->item(row);
                                  QString s;
                                  s = item->text();
                                  file3.resize(0);   // Delets all data from the textfile
                                  in3<<s;            //adds row to text file
                                  in3<<"\n";       //adds a line break
                              }
                              file3.close();
                       }
                      
                      JonBJ 1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Ruben12313
                        wrote on last edited by Ruben12313
                        #11

                        not sure if you need the other button to add an item but here:

                        void MainWindow::on_add_clicked()
                        {
                            QString fname1st = ("prac3.txt");
                            QFile file1st(fname1st);
                            if(file1st.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
                            {
                                QTextStream stream(&file1st);
                                QString file1st = ui->lineEdit_2->text();
                                stream << file1st <<"\n";
                            }
                        }
                        
                        1 Reply Last reply
                        0
                        • R Ruben12313

                          sure so in my header i created a pointer (int *amount).

                          on my code i used:

                          void MainWindow::on_pushButton_2_clicked()
                          {
                          //update listwidget
                                  QFile file3("prac3.txt");
                                  file3.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text);
                                  QTextStream in3(&file3);
                          
                                  QString line1 = ui->comboBox->currentText();
                                  QString amount = ui->lineEdit->text();   //number of item text
                          //detele old item data
                                  QString search(line1);
                                  QList<QListWidgetItem *> list = ui->listWidget->findItems(line1, Qt::MatchContains);
                                  for ( QListWidgetItem *item : list )
                                  delete item;
                          //add updated item with amount
                                  QStringList stringList;
                                  stringList << line1+"\t"+amount;
                                  ui->listWidget->addItems(stringList);
                          //Update the text file
                                  auto numRows = ui->listWidget->count();
                                  for(int row = 0; row < numRows; ++row)
                                  {
                                      auto item = ui->listWidget->item(row);
                                      QString s;
                                      s = item->text();
                                      file3.resize(0);   // Delets all data from the textfile
                                      in3<<s;            //adds row to text file
                                      in3<<"\n";       //adds a line break
                                  }
                                  file3.close();
                           }
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @Ruben12313
                          Is that supposed to be an answer to me? Have you verified it works when there are multiple items and you save to file? Whatever you see or find that QFile::resize(0) does why in the world do you call it for each line/row written?? And I find it very confusing that you open QIODevice::ReadWrite | QIODevice::Append: why open for append if you don't intend to append but rather overwrite the whole file? Never seen code written like this.

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            Ruben12313
                            wrote on last edited by Ruben12313
                            #13

                            Yes i can assure you this mini project works 100%, i save every row in to QString s, then delete the entire text file and add s back row by row. Append is a bit confusing for me but reading old post (forgot to save) I read its best to add it.

                            (https://ddgobkiprc33d.cloudfront.net/6e977f62-7100-4a2e-93c0-bbea3b949e80.png) Screenshot 2024-07-28 043538.png Screenshot 2024-07-28 043557.png

                            JonBJ 1 Reply Last reply
                            0
                            • R Ruben12313

                              Yes i can assure you this mini project works 100%, i save every row in to QString s, then delete the entire text file and add s back row by row. Append is a bit confusing for me but reading old post (forgot to save) I read its best to add it.

                              (https://ddgobkiprc33d.cloudfront.net/6e977f62-7100-4a2e-93c0-bbea3b949e80.png) Screenshot 2024-07-28 043538.png Screenshot 2024-07-28 043557.png

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

                              @Ruben12313
                              You (attempt to) "truncate" the file you are writing to as you write each line to it!

                              You have a stream named in3 which is only used for output.

                              If there are no lines now in the widget you fail to truncate/clear out any lines which were in the file (e.g. after deleting).

                              Etc.

                              It makes no sense. I leave it to you.

                              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