Qt 文本文件读写与保存
开发工具:VS2013 + QT5.8
设计UI界面,如下图所示
sample7_1QFile.h 头文件:
#pragma once#include <QtWidgets/QMainWindow>
#include "ui_sample7_1QFile.h"class sample7_1QFile : public QMainWindow
{Q_OBJECTpublic:sample7_1QFile(QWidget *parent = Q_NULLPTR);private:Ui::sample7_1QFileClass ui;private:bool openTextByIODevice(const QString& aFileName);bool saveTextByIODevice(const QString& aFileName);bool openTextByStream(const QString& aFileName);bool saveTextByStream(const QString& aFileName);private slots:void on_actOpen_IODevice_triggered();void on_actOpen_TextStream_triggered();void on_actSave_IODevice_triggered();void on_actSave_TextStream_triggered();};
sample7_1QFile.cpp 文件
#include "sample7_1QFile.h"#include <QDir>
#include <QFileDialog>
#include <QTextStream>
#include <QTextDocument>
#include <QTextBlock>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")sample7_1QFile::sample7_1QFile(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);this->setCentralWidget(ui.tabWidget);
}//用IODevice方式打开文本文件
bool sample7_1QFile::openTextByIODevice(const QString &aFileName)
{QFile aFile(aFileName);//aFile.setFileName(aFileName);if (!aFile.exists()) //文件不存在return false;if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))return false;ui.textEditDevice->setPlainText(aFile.readAll());//ui.textEditDevice->clear();//while (!aFile.atEnd())//{// QByteArray line = aFile.readLine();//自动添加 \n// QString str=QString::fromLocal8Bit(line); //从字节数组转换为字符串// str.truncate(str.length()-1); //去除结尾增加的空行// ui.textEditDevice->appendPlainText(str);//}aFile.close();ui.tabWidget->setCurrentIndex(0);return true;
}//用 QTextStream打开文本文件
bool sample7_1QFile::openTextByStream(const QString &aFileName)
{ QFile aFile(aFileName);if (!aFile.exists()) //文件不存在return false;if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))return false;QTextStream aStream(&aFile); //用文本流读取文件 aStream.setAutoDetectUnicode(true); //自动检测Unicode,才能正常显示文档内的汉字//ui.textEditStream->setPlainText(aStream.readAll());//读取全部内容//ui->textEditStream->clear();//清空//while (!aStream.atEnd())//{// str=aStream.readLine();//读取文件的一行// ui->textEditStream->appendPlainText(str); //添加到文本框显示//}aFile.close();//关闭文件ui.tabWidget->setCurrentIndex(1);return true;
}//用IODevice方式保存文本文件
bool sample7_1QFile::saveTextByIODevice(const QString &aFileName)
{ QFile aFile(aFileName);//aFile.setFileName(aFileName);if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))return false;QString str = ui.textEditDevice->toPlainText();//整个内容作为字符串QByteArray strBytes = str.toUtf8();//转换为字节数组//QByteArray strBytes=str.toLocal8Bit(); aFile.write(strBytes, strBytes.length());//写入文件 aFile.close();ui.tabWidget->setCurrentIndex(0);return true;
}//用QTextStream保存文本文件
bool sample7_1QFile::saveTextByStream(const QString &aFileName)
{QFile aFile(aFileName);if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))return false;QTextStream aStream(&aFile);//用文本流读取文件//aStream.setAutoDetectUnicode(true);//自动检测Unicode,才能正常显示文档内的汉字QString str = ui.textEditStream->toPlainText();//转换为字符串aStream << str; //写入文本流 //QTextDocument *doc; //文本对象//QTextBlock textLine; //文本中的一段//doc=ui->textEditStream->document(); //QPlainTextEdit 的内容保存在一个 QTextDocument 里//int cnt=doc->blockCount();//QTextDocument分块保存内容,文本文件就是硬回车符是一个block,//QString str;//for (int i=0; i<cnt; i++) //扫描所有 blobk//{// textLine=doc->findBlockByNumber(i);//用blobk编号获取block,就是获取一行// str=textLine.text(); //转换为文本,末尾无\n// aStream<<str<<"\n";//}aFile.close();//关闭文件return true;
}//打开文件
void sample7_1QFile::on_actOpen_IODevice_triggered()
{QString curPath = QDir::currentPath();//获取系统当前目录QString dlgTitle = "打开一个文件"; //对话框标题QString filter = "程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器QString aFileName = QFileDialog::getOpenFileName(this, dlgTitle, curPath, filter);if (aFileName.isEmpty())return;openTextByIODevice(aFileName);
}//打开文件
void sample7_1QFile::on_actOpen_TextStream_triggered()
{QString curPath = QDir::currentPath();//获取系统当前目录//调用打开文件对话框打开一个文件QString aFileName = QFileDialog::getOpenFileName(this, "打开一个文件", curPath,"程序文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)");if (aFileName.isEmpty()){return; //如果未选择文件,退出}openTextByStream(aFileName); //打开文件
}void sample7_1QFile::on_actSave_IODevice_triggered()
{QString curPath = QDir::currentPath();//获取系统当前目录QString dlgTitle = "另存为一个文件"; //对话框标题QString filter = "h文件(*.h);;c++文件(*.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);if (aFileName.isEmpty()){return;}saveTextByIODevice(aFileName);
}void sample7_1QFile::on_actSave_TextStream_triggered()
{QString curPath = QDir::currentPath();//获取系统当前目录QString dlgTitle = "另存为一个文件"; //对话框标题QString filter = "h文件(*.h);;c++文件(*.cpp);;文本文件(*.txt);;所有文件(*.*)"; //文件过滤器QString aFileName = QFileDialog::getSaveFileName(this, dlgTitle, curPath, filter);if (aFileName.isEmpty()){return;}saveTextByStream(aFileName);
}
main 函数:
#include "sample7_1QFile.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);sample7_1QFile w;w.show();return a.exec();
}