Results 1 to 3 of 3

Thread: Editing Text File in QT

  1. #1
    Join Date
    Jan 2017
    Posts
    4
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Editing Text File in QT

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



  2. #2
    Join Date
    Jan 2017
    Posts
    4
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Editing Text File in QT

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


    Attachment 12296 This is QTableWidget class

    Attachment 12297 this is when a new employee is to be added

    Attachment 12298 this is when e want to edit a employee data

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,325
    Thanks
    36
    Thanked 1,527 Times in 1,478 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Editing Text File in QT

    So "edit" means you remove the data for the person for which you have the name and then you modify the next person's data?

    Cheers,
    _

Similar Threads

  1. Quotation marks in QT text editing widgets
    By dalibor.free in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2011, 09:24
  2. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  3. Editing text file via console app
    By cejohnsonsr in forum Newbie
    Replies: 1
    Last Post: 30th August 2010, 21:39
  4. Editing in text box of web page : QWebView
    By girishgowda in forum Qt Programming
    Replies: 0
    Last Post: 13th July 2010, 11:42
  5. QTableWidgetItem Text Editing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 02:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.