Hey,
I am also from Bangalore.
So, For reading file you have to use the file handling. By using file handling, you can read the content of the text file and then set all the content on the textEdit. From that you can easily read the file. Use plainText to set the text.
Added after 10 minutes:
Try this code:---->
notepad.h
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QWidget>
#include<QFile>
#include<QTextStream>
namespace Ui {
class Notepad;
}
{
Q_OBJECT
public:
explicit Notepad
(QWidget *parent
= 0);
~Notepad();
private:
Ui::Notepad *ui;
};
#endif // NOTEPAD_H
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QWidget>
#include<QFile>
#include<QTextStream>
namespace Ui {
class Notepad;
}
class Notepad : public QWidget
{
Q_OBJECT
public:
explicit Notepad(QWidget *parent = 0);
~Notepad();
private:
Ui::Notepad *ui;
};
#endif // NOTEPAD_H
To copy to clipboard, switch view to plain text mode
notepad.cpp
#include "notepad.h"
#include "ui_notepad.h"
Notepad
::Notepad(QWidget *parent
) : ui(new Ui::Notepad)
{
ui->setupUi(this);
QFile file("C:\\Users\\Sonu\\Desktop\\test.txt");
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}
Notepad::~Notepad()
{
delete ui;
}
#include "notepad.h"
#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent) :
QWidget(parent),
ui(new Ui::Notepad)
{
ui->setupUi(this);
QString str;
QFile file("C:\\Users\\Sonu\\Desktop\\test.txt");
if(file.open(QIODevice::ReadOnly)){
QTextStream stream(&file);
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}
Notepad::~Notepad()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "notepad.h"
#include "ui_notepad.h"
Notepad
::Notepad(QWidget *parent
) : ui(new Ui::Notepad)
{
ui->setupUi(this);
QFile file("yourfile.txt");
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}
Notepad::~Notepad()
{
delete ui;
}
#include "notepad.h"
#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent) :
QWidget(parent),
ui(new Ui::Notepad)
{
ui->setupUi(this);
QString str;
QFile file("yourfile.txt");
if(file.open(QIODevice::ReadOnly)){
QTextStream stream(&file);
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}
Notepad::~Notepad()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
Bookmarks