Sunday, July 5, 2020

how to code in Qt to file read and write.

hi, to  all, first of all I am showing you how to code in Qt to file read and write.

In this there are one plainTextEdit to display ( read from file content and display ) and write to file( from plaintext edit to file ), one lineEdit to take path for read file and display into plainTextEdit, and also to write from plainTextEdit to file given by path in lilneEdit.

There are two pushButtons "write" and "read" which performs write to file and  read from file as  given path  in lineEdit.

to do this :-

1. File-> New -> Application -> Qt Widgets Application ->choose.
2. give suitable path and title. Mine title is "qfiles".
3. kit selection  -> next -> class information -> mine is MyMainWindow-> next
4. project management -> finish.

now create a Ui (usert interface) as follows  <image ui>.

now in "mymainwindow.h"

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include <QMainWindow>
#include <QTextStream>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QFileInfo>

namespace Ui {
class MyMainWindow;
}

class MyMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MyMainWindow(QWidget *parent = 0);
    ~MyMainWindow();

private slots:
    void on_pushButtonWrite_clicked();

    void on_pushButtonRead_clicked();

private:
    Ui::MyMainWindow *ui;
    QFileInfo *checkfile;
    QFile *file;
};

#endif // MYMAINWINDOW_H


in mymainwindow.cpp

#include "mymainwindow.h"
#include "ui_mymainwindow.h"

MyMainWindow::MyMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyMainWindow)
{
    ui->setupUi(this);
//    QFileInfo *checkfile = new QFileInfo;
    //QFileInfo *checkfile = nullptr;
    file = new QFile;
//    file = nullptr;
}

MyMainWindow::~MyMainWindow()
{
    delete ui;
}

void MyMainWindow::on_pushButtonWrite_clicked()
{
    QString filepath = ui->lineEditFile->text().trimmed();

    QDir d = QFileInfo(filepath).absoluteDir();
    QString absolute = d.absolutePath();
    //if(d.exists())
    if(!absolute.isEmpty())
    {
        file->setFileName(filepath);
        if(!file->open(QFile::WriteOnly | QFile::Text))
        {
             QMessageBox::information(this, "QDir", "the file  cannot be created");
        }

        else
        {
            QTextStream out(file);// = new QTextStream(file);
            QString str = ui->plainTextEdit->toPlainText();
            out << str;
            file->flush();
            file->close();

            QString name = QFileInfo(filepath).fileName();
            QMessageBox::information(this, "QFile", "File " + name +" has been written");
        }
    }
}

void MyMainWindow::on_pushButtonRead_clicked()
{
    QString filepath = ui->lineEditFile->text().trimmed();

    QDir d = QFileInfo(filepath).absoluteDir();
    QString absolute = d.absolutePath();
    //if(d.exists())
    if(!absolute.isEmpty())
    {
        file->setFileName(filepath);
        if(!file->open(QFile::ReadOnly | QFile::Text))
        {
             QMessageBox::information(this, "QDir", "the file  cannot be created");
        }

        else
        {
            QTextStream in(file);// = new QTextStream(file);
            QString text = in.readAll();
            ui->plainTextEdit->setPlainText(text);
            file->close();
        }
    }
}

now build and run this project :-

now  fill File path  and click on read pushbutton. If file path is valid then content of the text file will be displayed in plainTextEdit.
write some text in plainTextEdit and give file path, now content of plainTextEdit will be written in file as provided in file path  in lineEdit.



No comments:

Post a Comment