Hello I am a newbie in QT and doing some given task at work. I have a QTableWidget which populates the data from a text file. The QTableWidget ui has four push buttons(ADD,EDIT,DELETE,CLOSE). On clicking ADD/EDIT other ui opens up which Add's and Edit's the data on a push button SAVE. Clicking SAVE will add new entry IF ADD is clicked in text file while clicking SAVE will edit updated values if Edit is clicked.

Data in text file has the following format [Where one employee data has 8 fields]

Name=abc
Gender=Male
DOB=10/06/1992
Dept=Linux
Desgn=Junior
DOJ=28/11/2015
Location=CityName
Contact=1234567890
Name=xyz
Gender=Female
DOB=10/06/1992
Dept=OS
Desgn=Senior
DOJ=28/11/2015
Location=CityName
Contact=7894561330


and so on.



I want to edit the data from any selected name from QtableWidget by getting the index of the selected name from the file and edit the same. I m getting the index from the file. but trying hard to update the same Employee data by editing at the same index with all the fields in next 7 lines.


my code for editing is:

QString str_edit = "Name="+ui->LE_name->text(); //the name already is populated from text file to edit.
qDebug() << "Name to edit" << str_edit;

QStringList strlist;

QFile editfile("Data.txt");
QTextStream edit(&editfile);

if(editfile.open(QIODevice::ReadWrite | QFile::Text))
{
QString str_data;
while(!edit.atEnd())
{
str_data = edit.readLine().simplified();
strlist << str_data;
}

if(strlist.contains(str_edit))
{
int i = strlist.indexOf(str_edit);

//Gender logic here

for(int x = 0; x < 8; x++)
{
strlist.removeAt(i);
}

qDebug() << " Final Data : " << strlist << endl;

strlist.replace(i+2, QString("DOB="+ui->LE_dept->text()));

qDebug() << "*******Before Edit Data*******" << strlist.at(i+3);

strlist.replace(i+3, QString("Dept="+ui->LE_dept->text()));

qDebug() << "*******After Edit Data*******" << strlist.at(i+3);

strlist.replace(i+4, QString("Desgn="+ui->LE_desgn->text()));
strlist.replace(i+5, QString("DOJ="+ui->LE_dept->text()));
strlist.replace(i+6, QString("Location="+ui->LE_loc->text()));
strlist.replace(i+7, QString("Contact="+ui->LE_phone->text()));