Skip to content

Instantly share code, notes, and snippets.

@diniremix
Created December 4, 2011 06:06
Show Gist options
  • Save diniremix/1429370 to your computer and use it in GitHub Desktop.
Save diniremix/1429370 to your computer and use it in GitHub Desktop.
read file of text in Qt and show in QTextEdit
void MainWindow::readfile(){
QString filename="test.txt";
QFile file(filename);
if(!file.exists()){
qDebug() << "NO existe el archivo "<<filename;
}else{
qDebug() << filename<<" encontrado...";
}
QString line;
ui->textEdit->clear();
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&file);
while (!stream.atEnd()){
line = stream.readLine();
ui->textEdit->setText(ui->textEdit->toPlainText()+line+"\n");
qDebug() << "linea: "<<line;
}
}
file.close();
}
@nathan1324
Copy link

Thanks! This helped me for my own project.

@jmkerr
Copy link

jmkerr commented May 28, 2019

Try (l. 11)
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){ ui->textEdit->setText(file.readAll()); }

@mschollmeier
Copy link

Thanks, helped me too!

@fedorkobak
Copy link

Thanks, helped me too!

@Wael-ksila
Copy link

Thanks, helped me too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment