1

I wrote a Program in Qt 5.2.1 that writes some data into a file and now I want to read it and display it. ( in a text edit or any other widget)

Here is my code ( the part I thought is relevant ) -

But i don't get the desires result ... could you look into it and tell me what i an doing wrong

void MainWindow::on_Search_clicked()
{
   QString name ;
   name = ui->Search_name->text();

   QFile readfile("data.txt");
   if(!readfile.open(QIODevice::ReadOnly))
   {
       qDebug() << "error opening file: " << readfile.error();
       return;
   }

   QTextStream instream(&readfile);
   QString line = instream.readLine();

   // ui->text is a QPlainTextEdit*
   ui->text->insertPlainText(line);   

   readfile.close();
   return;
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
user121273
  • 59
  • 1
  • 2
  • 9
  • 1
    Some comments on style: your code has plenty of redundant constructs. `QFile` is a proper C++ resource class. You don't need to explicitly close it, you're not writing in C after all. You don't need to `return`. You don't need the text stream - simply invoke `readFile.readLine()`. Instead of `qDebug()`, a `qWarning()` would be more appropriate - after all, it's not merely a debugging information, it is a warning. – Kuba hasn't forgotten Monica Mar 12 '14 at 13:58

1 Answers1

1

You should use

void QPlainTextEdit::appendPlainText ( const QString & text ) [slot]

method, link.

4pie0
  • 29,204
  • 9
  • 82
  • 118