在Qt的开发过程中,有时我们需要把文本的内容输出到文件中方便查看,并且保存相应的文件格式可以用相对应的工具打开,比如.html, .txt, .csv等。这个时候用QTextStrem可以方便的解决这个问题。
下面请看示例:
//mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initConnect();
public slots:
void slotWirteFile();
void slotReadFile();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
initConnect();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initConnect()
{
connect(ui->pushButton_writeFile, SIGNAL(clicked()), this, SLOT(slotWirteFile()));
connect(ui->pushButton_readFile, SIGNAL(clicked()), this, SLOT(slotReadFile()));
}
void MainWindow::slotWirteFile()
{
QString strFilePath = "../QTextStreamWriteFile/write.html";
QFile file(strFilePath);
//方式: Append为追加, WriteOnly只写, ReadOnly只读,Truncate表示的是 将原文件内容清空,
//如果在工程文件下没有write.html文件,那么程序将创建该文件,若存在,则将原文件内容清空,
if(!file.open(QIODevice::WriteOnly |QIODevice::Truncate| QIODevice::Text))
//if(!file.open(QFile::WriteOnly |QFile::Truncate))
{
QMessageBox::critical(nullptr, "tips", "create file error");
return;
}
QTextStream out(&file);
//把数据写到html文件中
QString temp = "";
temp.append("<!DOCTYPE html>\n");
temp.append("<html lang='en'>\n");
temp.append("<head>\n");
temp.append("<meta charset='utf-8'>\n");
temp.append("<title>QTextStream example</title>");
temp.append("</head>\n");
temp.append("<body>\n");
temp.append("<h1 align=center> Hello World</h1>\n");
temp.append("</body>\r\n");
temp.append("</html>\r\n");
out.setCodec("UTF-8");
out << temp << endl;
qDebug() << "temp=======================" << temp;
out.flush();
file.close();
//注意写入方式的选择,注意写完后的关闭操作!
}
void MainWindow::slotReadFile()
{
QString path = QFileDialog::getOpenFileName(this, "open", "../");
if(path.isEmpty())
{
QMessageBox::critical(nullptr, "tips", "not select file");
return;
}
QFile file;
file.setFileName(path);
bool isOpen = file.open(QIODevice::ReadOnly);
if(isOpen)
{
QTextStream stream(&file);
//指定UTF-8编码
stream.setCodec("UTF-8");
QString str = stream.readAll();
qDebug() << "str=======================" << str;
ui->textEdit->setText(str);
}
else
{
QMessageBox::critical(nullptr, "tips", "open file error");
}
}
//main.cpp文件
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
写文件执行结果
读文件结果
参考:
http://blog.csdn.net/xgbing/article/details/7772953
http://blog.csdn.net/hudfang/article/details/42026257
http://ask.csdn.net/questions/56437
http://bbs.csdn.net/topics/390939537
https://blog.csdn.net/weixin_44011306/article/details/105578228
https://blog.csdn.net/a379039233/article/details/82528545
https://blog.csdn.net/weixin_39609623/article/details/83089994