一. 文本文件读写
- QFile
- QTextStream | 枚举值 | 描述 | | —- | —- | | QIODevice::ReadOnly | 以只读方式打开 | | QIODevice::WriteOnly | 以只写方式打开 | | QIODevice::ReadWrite | 以读写方式打开 | | QIODevice::Append | 以追加方式加到文件末尾 | | QIODevice::Truncate | 以重写方式打开,原有内容会被删除 | | QIODevice::Text | 在读取时,将行结束符转换成\n;在写入时,将行结束符转成本地格式,如 Win32 是 \r\n |
1. QFile
直接操作,相当于每人一瓶水。
- 读文件:
#include <QFileDialog>
QString curPath = QCoreApplication::applicationDirPath();
QString title = "打开文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString fileName = QFileDialog::getOpenFileName(this,
title,
curPath,
filter);
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
ui->plainTextEditIODevice->setPlainText(file.readAll());
file.close();
}
- 写文件:
QString curPath = QCoreApplication::applicationDirPath();
QString title = "保存文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString fileName = QFileDialog::getSaveFileName(this,
title,
curPath,
filter);
if (fileName.isEmpty()) return;
QString str = ui->plainTextEditIODevice->toPlainText();
QFile file(fileName);
QByteArray strBytes = str.toUtf8();
if (file.open(QIODevice::WriteOnly|QIODevice::Text)) {
file.write(strBytes, strBytes.length());
}
2. QTextStream
相当于装个饮水机,所有水都放入饮水机,然后一杯一杯弄,很好的控制
- 读操作:
QString curPath = QCoreApplication::applicationDirPath();
QString title = "保存文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString fileName = QFileDialog::getOpenFileName(this,
title,
curPath,
filter);
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly|QIODevice::Text)) {
QTextStream stream(&file);
// ui->plainTextEditTextStream->setPlainText(stream.readAll());
while (!stream.atEnd()) {
ui->plainTextEditTextStream->appendPlainText(stream.readLine());
}
file.close();
}
- 写操作:
QString curPath = QCoreApplication::applicationDirPath();
QString title = "保存文件";
QString filter = "文本文件(*.txt);;所有文件(*.*)";
QString fileName = QFileDialog::getSaveFileName(this,
title,
curPath,
filter);
if (fileName.isEmpty()) return;
QString str = ui->plainTextEditTextStream->toPlainText();
QFile file(fileName);
if (file.open(QIODevice::WriteOnly|QIODevice::Text)) {
QTextStream stream(&file);
stream << str;
}
二. 二进制文件读写
- QFile: 负责文件的 IO 设备接口,即与文件的物理交互。
QDataStream: 以数据流的方式读取或写入文件内容
- Qt 预定义编码
- 标准编码
1. Qt 预定义编码
- 写操作:
QString curPath = QCoreApplication::applicationDirPath();
QString fileName = QFileDialog::getSaveFileName(this,
"保存文件",
curPath,
"Qt预定义编码(*.stm)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::WriteOnly|QIODevice::Truncate)) {
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_9);
qint16 rowCount = theModel->rowCount();
qint16 colCount = theModel->columnCount();
stream << rowCount;
stream << colCount;
for (int i=0; i<colCount; i++)
stream << theModel->horizontalHeaderItem(i)->text();
// 数据区域
qint16 Depth;
qreal MeasuredDepth, Direction, Offset;
QString Quality;
bool Sampled;
for (int i=0; i<rowCount; i++) {
Depth = theModel->item(i, 0)->data(Qt::DisplayRole).toInt();
stream << Depth;
MeasuredDepth = theModel->item(i, 1)->data(Qt::DisplayRole).toFloat();
stream << MeasuredDepth;
Direction = theModel->item(i, 2)->data(Qt::DisplayRole).toFloat();
stream << Direction;
Offset = theModel->item(i, 3)->data(Qt::DisplayRole).toFloat();
stream << Offset;
Quality = theModel->item(i, 4)->data(Qt::DisplayRole).toString();
stream << Quality;
Sampled = (theModel->item(i, 5)->checkState() == Qt::Checked);
stream << Sampled;
}
file.close();
}
- 读操作:
QString curPath = QCoreApplication::applicationDirPath();
QString fileName = QFileDialog::getOpenFileName(this,
"打开文件",
curPath,
"Qt预定义编码(*.stm)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
QDataStream stream(&file);
stream.setVersion(QDataStream::Qt_5_9);
qint16 rowCount, coloumnCount;
stream >> rowCount;
stream >> coloumnCount;
resetTable(rowCount);
QString str;
for (int i=0; i<coloumnCount; i++)
stream >> str;
// 数据区域
qint16 Depth;
qreal MeasuredDepth, Direction, Offset;
QString Quality;
bool Sampled;
QStandardItem *item;
QModelIndex index;
for (int i=0; i<rowCount; i++) {
stream >> Depth;
index = theModel->index(i, 0);
item = theModel->itemFromIndex(index);
item->setData(Depth, Qt::DisplayRole);
stream >> MeasuredDepth;
index = theModel->index(i, 1);
item = theModel->itemFromIndex(index);
item->setData(MeasuredDepth, Qt::DisplayRole);
stream >> Direction;
index = theModel->index(i, 2);
item = theModel->itemFromIndex(index);
item->setData(Direction, Qt::DisplayRole);
stream >> Offset;
index = theModel->index(i, 3);
item = theModel->itemFromIndex(index);
item->setData(Offset, Qt::DisplayRole);
stream >> Quality;
index = theModel->index(i, 4);
item = theModel->itemFromIndex(index);
item->setData(Quality, Qt::DisplayRole);
stream >> Sampled;
index = theModel->index(i, 5);
item = theModel->itemFromIndex(index);
if (Sampled)
item->setCheckState(Qt::Checked);
else
item->setCheckState(Qt::Unchecked);
}
file.close();
}
2. 标准编码
- 写操作:
QString curPath = QCoreApplication::applicationDirPath();
QString fileName = QFileDialog::getSaveFileName(this,
"保存文件",
curPath,
"标准编码(*.dat)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::WriteOnly|QIODevice::Truncate)) {
QDataStream stream(&file);
stream.setByteOrder(QDataStream::LittleEndian);
qint16 rowCount = theModel->rowCount();
qint16 colCount = theModel->columnCount();
stream.writeRawData((char *)&rowCount, sizeof(qint16));
stream.writeRawData((char *)&colCount, sizeof(qint16));
QByteArray btArray;
for (int i=0; i<colCount; i++) {
QString str = theModel->horizontalHeaderItem(i)->text();
btArray = str.toUtf8();
stream.writeBytes(btArray, btArray.length());
}
// 数据区域
qint16 Depth;
qreal MeasuredDepth, Direction, Offset;
QString Quality;
bool Sampled;
for (int i=0; i<rowCount; i++) {
qDebug("row: %d", i);
Depth = theModel->item(i, 0)->data(Qt::DisplayRole).toInt();
qDebug("Depth: %d", Depth);
stream.writeRawData((char *)&Depth, sizeof(qint16));
MeasuredDepth = theModel->item(i, 1)->data(Qt::DisplayRole).toFloat();
stream.writeRawData((char *)&MeasuredDepth, sizeof(qreal));
Direction = theModel->item(i, 2)->data(Qt::DisplayRole).toFloat();
stream.writeRawData((char *)&Direction, sizeof(qreal));
Offset = theModel->item(i, 3)->data(Qt::DisplayRole).toFloat();
stream.writeRawData((char *)&Offset, sizeof(qreal));
Quality = theModel->item(i, 4)->data(Qt::DisplayRole).toString();
stream.writeBytes(Quality.toUtf8(), Quality.length());
Sampled = (theModel->item(i, 5)->checkState() == Qt::Checked);
qint8 yes = 0;
if (Sampled)
yes = 1;
stream.writeRawData((char *)&yes, sizeof(qint8));
}
file.close();
}
- 读操作:
QString curPath = QCoreApplication::applicationDirPath();
QString fileName = QFileDialog::getOpenFileName(this,
"打开文件",
curPath,
"标准编码(*.dat)");
if (fileName.isEmpty()) return;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
QDataStream stream(&file);
stream.setByteOrder(QDataStream::LittleEndian);
qint16 rowCount, colCount;
stream.readRawData((char *)&rowCount, sizeof(qint16));
stream.readRawData((char *)&colCount, sizeof(qint16));
resetTable(rowCount);
char *buf;
uint strlen;
for (int i=0; i<colCount; i++)
stream.readBytes(buf, strlen);
// 数据区域
qint16 Depth;
qreal MeasuredDepth, Direction, Offset;
QString Quality;
qint8 Sampled;
QStandardItem *item;
QModelIndex index;
for (int i=0; i<rowCount; i++) {
stream.readRawData((char *)&Depth, sizeof(qint16));
index = theModel->index(i, 0);
item = theModel->itemFromIndex(index);
item->setData(Depth, Qt::DisplayRole);
stream.readRawData((char *)&MeasuredDepth, sizeof(qreal));
index = theModel->index(i, 1);
item = theModel->itemFromIndex(index);
item->setData(MeasuredDepth, Qt::DisplayRole);
stream.readRawData((char *)&Direction, sizeof(qreal));
index = theModel->index(i, 2);
item = theModel->itemFromIndex(index);
item->setData(Direction, Qt::DisplayRole);
stream.readRawData((char *)&Offset, sizeof(qreal));
index = theModel->index(i, 3);
item = theModel->itemFromIndex(index);
item->setData(Offset, Qt::DisplayRole);
stream.readBytes(buf, strlen);
Quality = QString::fromLocal8Bit(buf, strlen);
index = theModel->index(i, 4);
item = theModel->itemFromIndex(index);
item->setData(Quality, Qt::DisplayRole);
stream.readRawData((char *)&Sampled, sizeof(qint8));
index = theModel->index(i, 5);
item = theModel->itemFromIndex(index);
if (Sampled == 1)
item->setCheckState(Qt::Checked);
else
item->setCheckState(Qt::Unchecked);
}
file.close();
}