PDA

View Full Version : QListWidget first line in file not read



vitalic
21st April 2020, 16:37
Hallo,

my program reads an txt file to a QListWidget, but the first row in the txt file is not shown when i open it. All the other rows are shown, except line 1.

Also you can see that there is an empty line after last filled row. in the txt there is no line or space or tab after "9".




void MainWindow::on_btnRead_clicked()
{
QFile file(pfad);
if(!file.open(QFile::ReadOnly | QFile::Text)){
QMessageBox::warning(this,"..","keine datei gefunden");
return;
}
QTextStream in(&file);
QString text = in.readLine();

while (!text.isNull()) {
text = in.readLine();
ui->listWidget->addItem(text);
}

file.close();
}

txt:

13421

output in program:

13422

Added after 1 50 minutes:

Code corrected, now it works:



void MainWindow::on_btnRead_clicked()
{
QFile file(pfad);
if(!file.open(QFile::ReadOnly | QFile::Text)){
QMessageBox::warning(this,"..","keine datei gefunden");
return;
}

QTextStream in(&file);
QString text;

while (!in.atEnd()) {
text = in.readLine();
ui->listWidget->addItem(text);
}

file.close();
}