Results 1 to 4 of 4

Thread: Editing a file in Qt4

  1. #1
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Editing a file in Qt4

    Hi,

    I have made a application that consists of 4 lineEdits such as number x, y , z the user will input the values to lineEdits and these values should be stored in a file.txt and also the user can change the values and these corresponding values has to be updated in the values the file looks like this

    1, 5.000.6.005, 8.340
    2, 45.220, 987.000, 64.000
    3, 21.000, 88.980, 73.000

    and so on

    how to eidt the values if user wants to change and here is the code snippet for accept the values from file then store

    Qt Code:
    1. void DatumForm::writefile()
    2. {
    3. QStringList lines;
    4. QString line;
    5. QFile file ( DATUMFILE );
    6. line.append( xdatumlineEdit->text() );
    7. line.append("," );
    8. line.append( ydatumlineEdit->text() );
    9. line.append("," );
    10. line.append( zdatumlineEdit->text() );
    11. lines += line;
    12. if ( file.open( QIODevice::ReadWrite | QIODevice::Append) )
    13. {
    14. QTextStream stream( &file );
    15. for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
    16. stream << *it << "\n";
    17. file.close();
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    now what code snippet is to be added to edit or overwrite the file. How to proceed with this.
    Thanks & Regards
    Sandeep G.R.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Editing a file in Qt4

    1. Open file for reading
    1. Read file.
    2. Populate lineEdits with current file content.
    3. Close file.

    When user selects "save" then:
    1. Open file for writing with QIODevice::Truncate
    2. Write lineEdit content to file.
    3. Close file.

    That is one way.
    Another way would be to parse the file and the lineEdits, and write the differences in to the file, but that would be more complected than the above method.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Editing a file in Qt4

    Hi,

    can you send a code snippet for QIODevice::Truncate..
    Thanks & Regards
    Sandeep G.R.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Editing a file in Qt4

    What do you mean?
    Your own code is a code snippet for it! - see QIODevice::open() documentation.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QFtp:upload file to server
    By ensky_cy in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 10:42
  2. Double Clicking Pro File
    By BalaQT in forum Installation and Deployment
    Replies: 2
    Last Post: 18th November 2009, 05:23
  3. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  4. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.