Results 1 to 7 of 7

Thread: Missing items in loadFile

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

    Default Missing items in loadFile

    Hello anyone,

    I want to load my contact.txt in the function loadFile.
    In my contact.txt i have seperated the items through tab.
    When open the file for reading he display only in QTreeWidget 4 items
    example:
    Drabo Hardenberg Dhr. J. Tuik 0523-678905.

    See code loadFile
    Qt Code:
    1. void MainWindow::loadFile(const QString &fileName)
    2. {
    3. QFile file( fileName );
    4. if( !file.open(QFile::ReadOnly))
    5. return;
    6.  
    7. QTextStream in( &file);
    8. QString line;
    9. QTreeWidgetItem *item = new QTreeWidgetItem( contactView );
    10.  
    11. do {
    12.  
    13. line = in.readLine();
    14. if(!line.isEmpty())
    15. {
    16. QStringList contact = line.split('\t', QString::SkipEmptyParts);
    17. QStringList::Iterator it = contact.begin();
    18. if(it != contact.end())
    19. {
    20. item->setText(0, (*it));
    21. ++it;
    22. }
    23. if(it != contact.end())
    24. {
    25. item->setText(1, (*it));
    26. ++it;
    27. }
    28. if(it != contact.end())
    29. {
    30. item->setText(2, (*it));
    31. ++it;
    32. }
    33. if(it != contact.end())
    34. {
    35. item->setText(3, (*it));
    36. ++it;
    37. }
    38. }
    39.  
    40. }while(!line.isEmpty());
    41.  
    42. file.close();
    43.  
    44. }
    To copy to clipboard, switch view to plain text mode 
    Is there something missing in the code

    Here is my code for the saveFile
    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. ind ++;
    16. }
    17. file.close();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Missing items in loadFile

    Hi,

    What is the exact problem...you want to display all the Items (If I count well, there are 6 "items" in the example you gave... ?

    Guilugi.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Missing items in loadFile

    seems like you only save 4 items to your file.
    Qt Code:
    1. for ( unsigned int i = 0; i < 4; i++ )
    To copy to clipboard, switch view to plain text mode 

    what is in your file after using safeFile? All the fields or just 4 of them?

    cheers

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

    Default Re: Missing items in loadFile

    In my contact.txt there are 4 items on each line seperate with a tab
    example of my contact.txt.
    Drabo Hardenberg Dhr. J. Tuik 0523-456789
    KMS Hardenberg Dhr. Kamphuis 0523-890567
    GMS Hardenberg Dhr. Huisman 0523-678945
    When i load the contact.txt the QTreeWidget display 1 line with items and not 3 lines.

  5. #5
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Missing items in loadFile

    Ok,

    The fact is you create only one TreeWidgetItem!
    You have to put your line in the loop, like this :


    Qt Code:
    1. void MainWindow::loadFile(const QString &fileName)
    2. {
    3. QFile file( fileName );
    4. if( !file.open(QFile::ReadOnly))
    5. return;
    6. QTextStream in( &file);
    7. QString line;
    8. do {
    9. line = in.readLine();
    10. if(!line.isEmpty())
    11. {
    12. QTreeWidgetItem *item = new QTreeWidgetItem( contactView );
    13.  
    14. QStringList contact = line.split('\t', QString::SkipEmptyParts);
    15. ....
    To copy to clipboard, switch view to plain text mode 

    This should work better

    Guilugi.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Missing items in loadFile

    you could try :
    !in.atEnd()}
    instead of
    !line.isEmpty());

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

    Default Re: Missing items in loadFile

    Thanks guilugi for your answer and time it works fine now.
    Everall i have also tested your sugestion but it didn't work.
    Thanks also for your sugestion.
    Bye

Similar Threads

  1. Some menubar items can not be clicked
    By richardander in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2009, 00:26
  2. Light items for the graphicsView
    By maverick_pol in forum Qt Programming
    Replies: 12
    Last Post: 1st November 2007, 18:51
  3. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 12:20

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.