PDA

View Full Version : How to insert values in QTreeWidget from Text file Using QlistIterator ?



Shahbaz
14th June 2015, 18:13
Iterator overwrites the values in QtreeWidget . My text file contains these three lines and only one line is showing

************text file **************
1200,Shahbaz,Ifc,laja
2001,ali,Step,kdh
1992,ikaka,psp,ayaj
****************************************
Kindly tell me how to avoid it .

My code is following

QString fileName = QFileDialog::getOpenFileName(this, tr ("Open File"), "C:\\", "All files (*.*);; Text File (*.txt);; IFC File (*.ifc);; STEP File (*.step);;XML files (*.xml);;Image files (*.jpg *.png)");

QFile file(fileName);

if (!file.open(QIODevice::ReadOnly | QIODevice ::Text))
QMessageBox::critical(this,tr("STOP"),tr("Error with loading"));

QTreeWidgetItem *item = new QTreeWidgetItem (ui->treeWidget);
QTextStream in(&file);
QString line;
// ui->textBrowser->setText(in.readAll());
do
{
line = in.readLine();

QList <QString> parts = line.split(",", QString::KeepEmptyParts);
QString L;
QListIterator<QString> Iter (parts);
while (Iter.hasNext())
{
for (int i=0 ; i<4; i++)
{
L = Iter.next();
item->setText(i,L);
}
}

} while (!in.atEnd());
}

wysota
14th June 2015, 20:16
Create a new item for each line you read instead of creating one item and overwriting its values upon every iteration.

ChrisW67
14th June 2015, 20:39
What are you trying to achieve with the while() and nested for()?

You might also think about what happens in the do {} while() when the file contains no lines.