PDA

View Full Version : Missing items in loadFile



dragon
2nd February 2006, 19:04
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


void MainWindow::loadFile(const QString &fileName)
{
QFile file( fileName );
if( !file.open(QFile::ReadOnly))
return;

QTextStream in( &file);
QString line;
QTreeWidgetItem *item = new QTreeWidgetItem( contactView );

do {

line = in.readLine();
if(!line.isEmpty())
{
QStringList contact = line.split('\t', QString::SkipEmptyParts);
QStringList::Iterator it = contact.begin();
if(it != contact.end())
{
item->setText(0, (*it));
++it;
}
if(it != contact.end())
{
item->setText(1, (*it));
++it;
}
if(it != contact.end())
{
item->setText(2, (*it));
++it;
}
if(it != contact.end())
{
item->setText(3, (*it));
++it;
}
}

}while(!line.isEmpty());

file.close();

}

Is there something missing in the code

Here is my code for the saveFile


void MainWindow::saveFile(const QString &fileName)
{

QFile file( fileName );
if ( !file.open(QFile::WriteOnly))
return;

QTextStream out( &file );

int ind = 0;
while(QTreeWidgetItem *item = contactView->topLevelItem(ind)) {
for ( unsigned int i = 0; i < 4; i++ )
out << item->text(i) << '\t';
out << '\n';
ind ++;
}
file.close();

}

guilugi
3rd February 2006, 12:05
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.

Everall
3rd February 2006, 14:24
seems like you only save 4 items to your file.

for ( unsigned int i = 0; i < 4; i++ )

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

cheers

dragon
3rd February 2006, 16:24
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.

guilugi
3rd February 2006, 16:37
Ok,

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




void MainWindow::loadFile(const QString &fileName)
{
QFile file( fileName );
if( !file.open(QFile::ReadOnly))
return;
QTextStream in( &file);
QString line;
do {
line = in.readLine();
if(!line.isEmpty())
{
QTreeWidgetItem *item = new QTreeWidgetItem( contactView );

QStringList contact = line.split('\t', QString::SkipEmptyParts);
....


This should work better :)

Guilugi.

Everall
3rd February 2006, 16:38
you could try :
!in.atEnd()}
instead of
!line.isEmpty());

dragon
3rd February 2006, 18:14
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