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. Getting the rows in a qtablewidget to new lines in a textfile in qt c++
QtWS25 Last Chance

Getting the rows in a qtablewidget to new lines in a textfile in qt c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorc++qstringrownew line
4 Posts 3 Posters 2.0k 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.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by Lasith
    #1

    follwing code is used by me to read tablewidget rows and get the contents into a Qstring and then put the string into a textfile. Currently all the rows appear as single line in the textfile! How can I display each row as separate line?

    void MainWindow::on_pushButton_3_clicked()
    {

        QString itabtext = " ";
      for(int i=0;i<ui->tableWidget_2->rowCount();i++){
          for(int k=0;k<ui->tableWidget_2->columnCount();k++){
              QTableWidgetItem *itab = ui->tableWidget_2->item(i,k);
    
               itabtext+=itab->text();
    
          }
       
      }
    
    
      QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");
    
      if(mFile.open(QIODevice::ReadOnly|QIODevice::WriteOnly|QIODevice::Text)){
        QTextStream out(&mFile);
     
        out<<itabtext;
      
        mFile.flush();
        mFile.close();
    

    }

    }

    joeQJ 1 Reply Last reply
    0
    • L Lasith

      follwing code is used by me to read tablewidget rows and get the contents into a Qstring and then put the string into a textfile. Currently all the rows appear as single line in the textfile! How can I display each row as separate line?

      void MainWindow::on_pushButton_3_clicked()
      {

          QString itabtext = " ";
        for(int i=0;i<ui->tableWidget_2->rowCount();i++){
            for(int k=0;k<ui->tableWidget_2->columnCount();k++){
                QTableWidgetItem *itab = ui->tableWidget_2->item(i,k);
      
                 itabtext+=itab->text();
      
            }
         
        }
      
      
        QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");
      
        if(mFile.open(QIODevice::ReadOnly|QIODevice::WriteOnly|QIODevice::Text)){
          QTextStream out(&mFile);
       
          out<<itabtext;
        
          mFile.flush();
          mFile.close();
      

      }

      }

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by joeQ
      #2

      @Lasith hi,friend,welcome.

      I modified your code snippet at below.

      void MainWindow::on_pushButton_3_clicked()
      {
          QStringList lines; ///< First note, I used the QStringList
          for(int i=0;i<ui->tableWidget_2->rowCount();i++){
              for(int k=0;k<ui->tableWidget_2->columnCount();k++){
                  QTableWidgetItem *itab = ui->tableWidget_2->item(i,k);
                  lines += itab->text();
              }
      
          }
      
          /** you can use any char */
          QString str = lines.join(QChar('-'));
          ui->lineEdit_2->setText(str); ///< why didn't you to use QTextEdit ? 
      
          QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");
          if(mFile.open(QIODevice::ReadOnly|QIODevice::WriteOnly|QIODevice::Text)){
              QTextStream out(&mFile);
      
              /** write every line of lines to file */
              for(const QString &line:lines){
                  out << line << endl; ///< add endl
              }
      
              mFile.flush();
              mFile.close();
          }
      
      }
      

      Just do it!

      1 Reply Last reply
      2
      • L Offline
        L Offline
        Lasith
        wrote on last edited by
        #3

        thanx! but I found the solution :D
        void MainWindow::on_pushButton_3_clicked()
        {

        QString itabtext = " ";
        

        for(int i=0;i<ui->tableWidget_2->rowCount();i++){
        for(int k=0;k<ui->tableWidget_2->columnCount();k++){
        QTableWidgetItem *itab = ui->tableWidget_2->item(i,k);
        if(k==2){
        itabtext+=itab->text()+"\n";
        break;
        }
        itabtext+=itab->text();

          }
        

        }

        QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");
        QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");

        if(mFile.open(QIODevice::ReadOnly|QIODevice::WriteOnly|QIODevice::Text)){
        QTextStream out(&mFile);

        out<<itabtext;
        
        mFile.flush();
        mFile.close();
        

        }

        }

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          While I'm here, you can use https://github.com/VSRonin/Qt-Model-Serialisation/tree/dev

          CsvModelSerialiser modelSaver;
          modelSaver->setModel(ui->tableWidget_2->model());
          modelSaver->setCsvSeparator(" ");
          QFile mFile("C:/Users/Desktop/Qt/textfiles/test.txt");
          modelSaver->saveModel(&mFile);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

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