Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • 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.1k 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 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
                        • Unsolved