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



  • 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();
    

    }

    }



  • @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();
        }
    
    }
    


  • 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();
    

    }

    }



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


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.