How to read data from a file in Qt
Jump to navigation
Jump to search
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]
- include <QFile>
- include <QApplication>
- include <QLabel>
- include <QString>
- 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();
}