dragon
2nd February 2006, 18: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();
}
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();
}