Results 1 to 5 of 5

Thread: saveFile

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default saveFile

    Hello anyone,

    I have a QTreeWidget with a saveFile function.
    I want to save 4 items in a text file with a tab between the items on a row.
    This code save the items. 1 item on 1 row.
    See code:
    Qt Code:
    1. void MainWindow::saveFile(const QString &fileName)
    2. {
    3.  
    4. QFile file( fileName );
    5. if ( !file.open(QFile::WriteOnly))
    6. return;
    7.  
    8. QTextStream out( &file );
    9.  
    10. int ind = 0;
    11. while(QTreeWidgetItem *item = contactView->topLevelItem(ind)) {
    12. for ( unsigned int i = 0; i < 4; i++ )
    13. out << item->text(i) << "\n";
    14.  
    15. ind ++;
    16. }
    17. file.close();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    Must i use QStringlist with join or can i change the code a litle bit.

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: saveFile

    Try something like this:

    Qt Code:
    1. void MainWindow::saveFile(const QString &fileName)
    2. {
    3.  
    4. QFile file( fileName );
    5. if ( !file.open(QFile::WriteOnly))
    6. return;
    7.  
    8. QTextStream out( &file );
    9.  
    10. int ind = 0;
    11. while(QTreeWidgetItem *item = contactView->topLevelItem(ind)) {
    12. for ( unsigned int i = 0; i < 4; i++ )
    13. out << item->text(i) << '\t';
    14. out << '\n';
    15.  
    16. ind ++;
    17. }
    18. file.close();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: saveFile

    Thanks axeljaeger for your answer.
    It works fine now in the textfile with tabs between the item's.

  4. #4
    Join Date
    Jan 2006
    Posts
    46
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: saveFile

    Please, Dragon, how did you manage to keep the indentation
    in your code? I've tried both tabs and white spaces and my
    code comes out flat on the left margin...?

    Mariane

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: saveFile

    Quote Originally Posted by Mariane
    how did you manage to keep the indentation
    in your code? I've tried both tabs and white spaces and my
    code comes out flat on the left margin...?
    That's what [ code ] tags are for, you can insert them using # button in the editor or just type them.

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.