How to read data from a file in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract

Template:ArticleMetaData

Description[edit | edit source]

  • Make a text file and give full path in the program.
  • The following code snippet would read this file and display its content as a text label.

Source File[edit | edit source]

  1. include <QFile>
  2. include <QApplication>
  3. include <QLabel>
  4. include <QString>
  5. include <QTextStream>

int main(int argc, char *argv[]) { QApplication app(argc, argv); QFile file("c://in.txt"); file.open(QIODevice::ReadOnly | QIODevice::Text);


QTextStream in(&file); QString line = in.readLine();

QLabel label(line); label.show();

return app.exec(); }

Screenshot[edit | edit source]

File:Fileread.jpg