栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Qt学习(四):qt读写文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt学习(四):qt读写文件

知识点
  1. QFile读写文件
  2. QFileInfo获取文件信息
  3. QDataStream读写文件
  4. QTextStream读写文件
  5. 内存文件QBuffer读写文件
结果演示

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_ReadButton_clicked();

    void on_WriteButton_clicked();

    //通过文件流读写文件
    void writeDate();
    void readDate();

    //第三种方式通过QTextStream读写文件
    void writeDateTextStre();
    void readDateTextStre();

    //第四种方式通过内存文件QBuffer读写文件
    void WriteReadBuffer();

private:
    Ui::Widget *ui;

};

#endif // WIDGET_H

widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include //内存文件

#define cout qDebug() << "[" << __FILE__ <<":" << __LINE__ << "]"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //第二种方式通过QDataStream读写文件
    writeDate();
    readDate();

    //第三种方式通过QTextStream读写文件
    writeDateTextStre();
    readDateTextStre();

    //第四种方式通过内存文件QBuffer读写文件
    WriteReadBuffer();
}

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

void Widget::on_ReadButton_clicked()
{
    QString path=QFileDialog::getOpenFileName(this, tr("Open File"),
                                              "../",
                                              tr("Text files (*.txt);;Images (*.png *.xpm *.jpg)"));
    //路径打开了
    if(!path.isEmpty()){
        //文件对象
        QFile file(path);
        bool openOk=file.open(QIODevice::ReadOnly);
        if(openOk){

#if 0
            //读文件,默认只识别utf8编码
            QByteArray array=file.readAll();
            //显示到编辑区
            ui->textEdit->setText(array);
#endif
            QByteArray array;
            while(!file.atEnd()){
                //读一行
                array+=file.readLine();
            }
            ui->textEdit->setText(array);

        }
        file.close();
    }

    //QFileInfo获取文件信息
    QFileInfo FileDate(path);
    qDebug()<textEdit->toPlainText();

            //显示中文,转为uft8
            QByteArray strArray=str.toUtf8();
            file.write(str.toUtf8());
        }
        file.close();
    }

}

//通过文件流读写文件
void Widget::writeDate(){
    QString path("../dateTxt.txt");
    //qDebug()<>str>>a;
        //qDebug() << str.toUtf8().data() << a;
        cout << str.toUtf8().data() << a;

        file.close();
    }
}

 //第三种方式通过QTextStream读写文件
void Widget::writeDateTextStre(){
    QString path("../DemoTxt.txt");
    //qDebug()<>str>>a;
    //qDebug() << str.toUtf8().data() << a;
    cout << str.toUtf8().data() << a;

    merbuffer.close();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691471.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号