点击上方“Qt学视觉”,选择“星标”公众号重磅干货,第一时间送达
想要学习的同学们还请认真阅读每篇文章,相信你一定会有所收获
除了文本文件之外,其他需要按照一定的格式定义读写的文件都称为二进制文件,每种格式的二进制文件都有自己的格式定义,写入数据时按照一定的顺序写入,读出时也按照相应的顺序读出
Qt使用QFile和QDataStream进行二进制数据文件的读写,QFile负责文件的IO设备接口,即与文件的物理交互,QDataStream以数据流的方式读取文件内容或写入文件内容
紧接着上一节的界面往上加这节的界面
头文件
#pragma once#include #include "ui_QGuiFileSys.h"#include "QComboBoxDelegate.h"#include "QFloatSpinDelegate.h"#include "QIntSpinDelegate.h"#include #include #include #include #define FixedColumnCount 6 //文件固定6行class QGuiFileSys : public QMainWindow{ Q_OBJECTpublic: QGuiFileSys(QWidget *parent = Q_NULLPTR); ~QGuiFileSys();private: Ui::QGuiFileSys 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 actOpenIODevice_triggered(); void actSaveIODevice_triggered(); void actOpenTextStream_triggered(); void actSaveTextStream_triggered();private: //用于状态栏的信息显示 QLabel* LabCellPos; //当前单元格行列号 QLabel* LabCellText; //当前单元格内容 QIntSpinDelegate intSpinDelegate; //整型数 QFloatSpinDelegate floatSpinDelegate; //浮点数 QComboBoxDelegate comboBoxDelegate; //列表选择 QStandardItemModel* theModel;//数据模型 QItemSelectionModel* theSelection;//Item选择模型 void resetTable(int aRowCount); //表格复位,设定行数 bool saveDataAsStream(QString& aFileName);//将数据保存为数据流文件 bool openDataAsStream(QString& aFileName);//读取数据流文件 bool saveBinaryFile(QString& aFileName);//保存为二进制文件 bool openBinaryFile(QString& aFileName);//打开二进制文件private slots: void theSelection_currentChanged(const QModelIndex& current, const QModelIndex& previous); void actTabReset_triggered(); void actOpenStm_triggered(); void actSaveStm_triggered(); void actOpenBin_triggered(); void actSaveBin_triggered(); void actAppend_triggered(); void actInsert_triggered(); void actDelete_triggered(); void actAlignLeft_triggered(); void actAlignCenter_triggered(); void actAlignRight_triggered(); void actFontBold_triggered(bool checked);}
源文件
#include "QGuiFileSys.h"#include #include #include #include #include #include #include #include #include #include #pragma execution_character_set("utf-8")QGuiFileSys::QGuiFileSys(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this); connect(ui.actOpenIODevice, SIGNAL(triggered()), this, SLOT(actOpenIODevice_triggered())); connect(ui.actSaveIODevice, SIGNAL(triggered()), this, SLOT(actSaveIODevice_triggered())); connect(ui.actOpenTextStream, SIGNAL(triggered()), this, SLOT(actOpenTextStream_triggered())); connect(ui.actSaveTextStream, SIGNAL(triggered()), this, SLOT(actSaveTextStream_triggered())); connect(ui.actTabReset, SIGNAL(triggered()), this, SLOT(actTabReset_triggered())); connect(ui.actOpenStm, SIGNAL(triggered()), this, SLOT(actOpenStm_triggered())); connect(ui.actSaveStm, SIGNAL(triggered()), this, SLOT(actSaveStm_triggered())); connect(ui.actOpenBin, SIGNAL(triggered()), this, SLOT(actOpenBin_triggered())); connect(ui.actSaveBin, SIGNAL(triggered()), this, SLOT(actSaveBin_triggered())); connect(ui.actAppend, SIGNAL(triggered()), this, SLOT(actAppend_triggered())); connect(ui.actInsert, SIGNAL(triggered()), this, SLOT(actInsert_triggered())); connect(ui.actDelete, SIGNAL(triggered()), this, SLOT(actDelete_triggered())); connect(ui.actAlignLeft, SIGNAL(triggered()), this, SLOT(actAlignLeft_triggered())); connect(ui.actAlignCenter, SIGNAL(triggered()), this, SLOT(actAlignCenter_triggered())); connect(ui.actAlignRight, SIGNAL(triggered()), this, SLOT(actAlignRight_triggered())); connect(ui.actFontBold, SIGNAL(triggered(bool)), this, SLOT(actFontBold_triggered(bool))); theModel = new QStandardItemModel(5, FixedColumnCount, this); //创建数据模型 QStringList headerList; headerList << "Depth" << "Measured Depth" << "Direction" << "Offset" << "Quality" << "Sampled"; theModel->setHorizontalHeaderLabels(headerList); //设置表头文字 theSelection = new QItemSelectionModel(theModel);//Item选择模型 connect(theSelection, SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(theSelection_currentChanged(const QModelIndex&, const QModelIndex&))); //为tableView设置数据模型 ui.tableView->setModel(theModel); //设置数据模型 ui.tableView->setSelectionModel(theSelection);//设置选择模型 //为各列设置自定义代理组件 ui.tableView->setItemDelegateForColumn(0, &intSpinDelegate); //测深,整数 ui.tableView->setItemDelegateForColumn(1, &floatSpinDelegate); //浮点数 ui.tableView->setItemDelegateForColumn(2, &floatSpinDelegate); //浮点数 ui.tableView->setItemDelegateForColumn(3, &floatSpinDelegate); //浮点数 ui.tableView->setItemDelegateForColumn(4, &comboBoxDelegate); //Combbox选择型 resetTable(5); //表格复位 //创建状态栏组件 LabCellPos = new QLabel("当前单元格:", this); LabCellPos->setMinimumWidth(180); LabCellPos->setAlignment(Qt::AlignHCenter); LabCellText = new QLabel("单元格内容:", this); LabCellText->setMinimumWidth(200); ui.statusBar->addWidget(LabCellPos); ui.statusBar->addWidget(LabCellText);}QGuiFileSys::~QGuiFileSys(){}bool QGuiFileSys::openTextByIODevice(const QString& aFileName){ //用IODevice方式打开文本文件 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;}bool QGuiFileSys::saveTextByIODevice(const QString& aFileName){ //用IODevice方式保存文本文件 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;}bool QGuiFileSys::openTextByStream(const QString& aFileName){ //用 QTextStream打开文本文件 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;}bool QGuiFileSys::saveTextByStream(const QString& aFileName){ //用QTextStream保存文本文件 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 //{ // textLine=doc->findBlockByNumber(i);//用blobk编号获取block,就是获取一行 // str=textLine.text(); //转换为文本,末尾无\n // aStream< //} aFile.close();//关闭文件 return true;}void QGuiFileSys::actOpenIODevice_triggered(){ //获取系统当前目录 QString curpath = QDir::currentPath(); //调用打开文件对话框打开一个文件 QString aFileName = QFileDialog::getOpenFileName(this, "打开一个文件", curpath, "程序文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)"); if (aFileName.isEmpty()) return; //如果未选择文件,退出 openTextByIODevice(aFileName); //打开文件}void QGuiFileSys::actSaveIODevice_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 QGuiFileSys::actOpenTextStream_triggered(){ QString curpath = QDir::currentPath();//获取系统当前目录 //调用打开文件对话框打开一个文件 QString aFileName = QFileDialog::getOpenFileName(this, "打开一个文件", curpath, "程序文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)"); if (aFileName.isEmpty()) return; //如果未选择文件,退出 openTextByStream(aFileName); //打开文件}void QGuiFileSys::actSaveTextStream_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);}//表格复位,设定行数void QGuiFileSys::resetTable(int aRowCount){ //表格复位,先删除所有行,再设置新的行数,表头不变 //QStringList headerList; //headerList< //theModel->setHorizontalHeaderLabels(headerList); //设置表头文字 theModel->removeRows(0, theModel->rowCount()); //删除所有行 theModel->setRowCount(aRowCount);//设置新的行数 QString str = theModel->headerData(theModel->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString(); for (int i = 0; i < theModel->rowCount(); i++) { //设置最后一列 QModelIndex index = theModel->index(i, FixedColumnCount - 1); //获取模型索引 QStandardItem* aItem = theModel->itemFromIndex(index); //获取item aItem->setCheckable(true); aItem->setData(str, Qt::DisplayRole); aItem->setEditable(false); //不可编辑 }}//将数据保存为数据流文件bool QGuiFileSys::saveDataAsStream(QString& aFileName){ //将模型数据保存为Qt预定义编码的数据文件 QFile aFile(aFileName); //以文件方式读出 if (!(aFile.open(QIODevice::WriteOnly | QIODevice::Truncate))) return false; QDataStream aStream(&aFile); aStream.setVersion(QDataStream::Qt_5_9); //设置版本号,写入和读取的版本号要兼容 qint16 rowCount = theModel->rowCount(); //数据模型行数 qint16 colCount = theModel->columnCount(); //数据模型列数 aStream << rowCount; //写入文件流,行数 aStream << colCount;//写入文件流,列数 //获取表头文字 for (int i = 0; i < theModel->columnCount(); i++) { QString str = theModel->horizontalHeaderItem(i)->text();//获取表头文字 aStream << str; //字符串写入文件流,Qt预定义编码方式 } //获取数据区的数据 for (int i = 0; i < theModel->rowCount(); i++) { QStandardItem* aItem = theModel->item(i, 0); //测深 qint16 ceShen = aItem->data(Qt::DisplayRole).toInt(); aStream << ceShen;// 写入文件流,qint16 aItem = theModel->item(i, 1); //垂深 qreal chuiShen = aItem->data(Qt::DisplayRole).toFloat(); aStream << chuiShen;//写入文件流, qreal aItem = theModel->item(i, 2); //方位 qreal fangWei = aItem->data(Qt::DisplayRole).toFloat(); aStream << fangWei;//写入文件流, qreal aItem = theModel->item(i, 3); //位移 qreal weiYi = aItem->data(Qt::DisplayRole).toFloat(); aStream << weiYi;//写入文件流, qreal aItem = theModel->item(i, 4); //固井质量 QString zhiLiang = aItem->data(Qt::DisplayRole).toString(); aStream << zhiLiang;// 写入文件流,字符串 aItem = theModel->item(i, 5); //测井 bool quYang = (aItem->checkState() == Qt::Checked); aStream << quYang;// 写入文件流,bool型 } aFile.close(); return true;}//读取数据流文件bool QGuiFileSys::openDataAsStream(QString& aFileName){ //从Qt预定义流文件读入数据 QFile aFile(aFileName); if (!aFile.open(QIODevice::ReadOnly)) return false; QDataStream aStream(&aFile);//用文本流读取文件 aStream.setVersion(QDataStream::Qt_5_12);//设置流文件版本号 qint16 rowCount, colCount; aStream >> rowCount;//读取行数 aStream >> colCount;//读取列数 this->resetTable(rowCount);//表格复位 //读取表头文字 QString str; for (int i = 0; i < colCount; i++) { aStream >> str;//读取表头字符串 } //获取数据区文字 qint16 ceShen; qreal chuiShen; qreal fangWei; qreal weiYi; QString zhiLiang; bool quYang; QStandardItem* aItem; QModelIndex index; for (int i = 0; i < rowCount; i++) { aStream >> ceShen;//读取测深, qint16 index = theModel->index(i, 0); aItem = theModel->itemFromIndex(index); aItem->setData(ceShen, Qt::DisplayRole); aStream >> chuiShen;//垂深,qreal index = theModel->index(i, 1); aItem = theModel->itemFromIndex(index); aItem->setData(chuiShen, Qt::DisplayRole); aStream >> fangWei;//方位,qreal index = theModel->index(i, 2); aItem = theModel->itemFromIndex(index); aItem->setData(fangWei, Qt::DisplayRole); aStream >> weiYi;//位移,qreal index = theModel->index(i, 3); aItem = theModel->itemFromIndex(index); aItem->setData(weiYi, Qt::DisplayRole); aStream >> zhiLiang;//固井质量,QString index = theModel->index(i, 4); aItem = theModel->itemFromIndex(index); aItem->setData(zhiLiang, Qt::DisplayRole); aStream >> quYang;//bool index = theModel->index(i, 5); aItem = theModel->itemFromIndex(index); if (quYang) aItem->setCheckState(Qt::Checked); else aItem->setCheckState(Qt::Unchecked); } aFile.close(); return true;}//保存为二进制文件bool QGuiFileSys::saveBinaryFile(QString& aFileName){ //保存为纯二进制文件 QFile aFile(aFileName); //以文件方式读出 if (!(aFile.open(QIODevice::WriteOnly))) return false; QDataStream aStream(&aFile); //用文本流读取文件 //aStream.setVersion(QDataStream::Qt_5_9); //无需设置数据流的版本 aStream.setByteOrder(QDataStream::LittleEndian);//windows平台 //aStream.setByteOrder(QDataStream::BigEndian);//QDataStream::LittleEndian qint16 rowCount = theModel->rowCount(); qint16 colCount = theModel->columnCount(); aStream.writeRawData((char*)&rowCount, sizeof(qint16)); //写入文件流 aStream.writeRawData((char*)&colCount, sizeof(qint16));//写入文件流 //获取表头文字 QByteArray btArray; QStandardItem* aItem; for (int i = 0; i < theModel->columnCount(); i++) { aItem = theModel->horizontalHeaderItem(i); //获取表头item QString str = aItem->text(); //获取表头文字 btArray = str.toUtf8(); //转换为字符数组 aStream.writeBytes(btArray, btArray.length()); //写入文件流,长度uint型,然后是字符串内容 } //获取数据区文字, qint8 yes = 1, no = 0; //分别代表逻辑值 true和false for (int i = 0; i < theModel->rowCount(); i++) { aItem = theModel->item(i, 0); //测深 qint16 ceShen = aItem->data(Qt::DisplayRole).toInt();//qint16类型 aStream.writeRawData((char*)&ceShen, sizeof(qint16));//写入文件流 aItem = theModel->item(i, 1); //垂深 qreal chuiShen = aItem->data(Qt::DisplayRole).toFloat();//qreal 类型 aStream.writeRawData((char*)&chuiShen, sizeof(qreal));//写入文件流 aItem = theModel->item(i, 2); //方位 qreal fangWei = aItem->data(Qt::DisplayRole).toFloat(); aStream.writeRawData((char*)&fangWei, sizeof(qreal)); aItem = theModel->item(i, 3); //位移 qreal weiYi = aItem->data(Qt::DisplayRole).toFloat(); aStream.writeRawData((char*)&weiYi, sizeof(qreal)); aItem = theModel->item(i, 4); //固井质量 QString zhiLiang = aItem->data(Qt::DisplayRole).toString(); btArray = zhiLiang.toUtf8(); aStream.writeBytes(btArray, btArray.length()); //写入长度,uint,然后是字符串 //aStream.writeRawData(btArray,btArray.length());//对于字符串,应使用writeBytes()函数 aItem = theModel->item(i, 5); //测井取样 bool quYang = (aItem->checkState() == Qt::Checked); //true or false if (quYang) aStream.writeRawData((char*)&yes, sizeof(qint8)); else aStream.writeRawData((char*)&no, sizeof(qint8)); } aFile.close(); return true;}//打开二进制文件bool QGuiFileSys::openBinaryFile(QString& aFileName){ //打开二进制文件 QFile aFile(aFileName); //以文件方式读出 if (!(aFile.open(QIODevice::ReadOnly))) return false; QDataStream aStream(&aFile); //用文本流读取文件 //aStream.setVersion(QDataStream::Qt_5_9); //设置数据流的版本 aStream.setByteOrder(QDataStream::LittleEndian); //aStream.setByteOrder(QDataStream::BigEndian); qint16 rowCount, colCount; aStream.readRawData((char*)&rowCount, sizeof(qint16)); aStream.readRawData((char*)&colCount, sizeof(qint16)); this->resetTable(rowCount); //获取表头文字,但是并不利用 char* buf; uint strLen; //也就是 quint32 for (int i = 0; i < colCount; i++) { aStream.readBytes(buf, strLen);//同时读取字符串长度,和字符串内容 QString str = QString::fromLocal8Bit(buf, strLen); //可处理汉字 } //获取数据区数据 QStandardItem* aItem; qint16 ceShen; qreal chuiShen; qreal fangWei; qreal weiYi; QString zhiLiang; qint8 quYang; //分别代表逻辑值 true和false QModelIndex index; for (int i = 0; i < rowCount; i++) { aStream.readRawData((char*)&ceShen, sizeof(qint16)); //测深 index = theModel->index(i, 0); aItem = theModel->itemFromIndex(index); aItem->setData(ceShen, Qt::DisplayRole); aStream.readRawData((char*)&chuiShen, sizeof(qreal)); //垂深 index = theModel->index(i, 1); aItem = theModel->itemFromIndex(index); aItem->setData(chuiShen, Qt::DisplayRole); aStream.readRawData((char*)&fangWei, sizeof(qreal)); //方位 index = theModel->index(i, 2); aItem = theModel->itemFromIndex(index); aItem->setData(fangWei, Qt::DisplayRole); aStream.readRawData((char*)&weiYi, sizeof(qreal)); //位移 index = theModel->index(i, 3); aItem = theModel->itemFromIndex(index); aItem->setData(weiYi, Qt::DisplayRole); aStream.readBytes(buf, strLen);//固井质量 zhiLiang = QString::fromLocal8Bit(buf, strLen); index = theModel->index(i, 4); aItem = theModel->itemFromIndex(index); aItem->setData(zhiLiang, Qt::DisplayRole); aStream.readRawData((char*)&quYang, sizeof(qint8)); //测井取样 index = theModel->index(i, 5); aItem = theModel->itemFromIndex(index); if (quYang == 1) aItem->setCheckState(Qt::Checked); else aItem->setCheckState(Qt::Unchecked); } aFile.close(); return true;}void QGuiFileSys::theSelection_currentChanged(const QModelIndex& current, const QModelIndex& previous){ Q_UNUSED(previous); if (current.isValid()) { LabCellPos->setText(QString::asprintf("当前单元格:%d行,%d列", current.row(), current.column())); QStandardItem* aItem; aItem = theModel->itemFromIndex(current); //从模型索引获得Item this->LabCellText->setText("单元格内容:" + aItem->text()); QFont font = aItem->font(); ui.actFontBold->setChecked(font.bold()); }}void QGuiFileSys::actTabReset_triggered(){ //表格复位 resetTable(10);}void QGuiFileSys::actOpenStm_triggered(){ QString curPath = QDir::currentPath(); //调用打开文件对话框打开一个文件 QString aFileName = QFileDialog::getOpenFileName(this, tr("打开一个文件"), curPath, "流数据文件(*.stm)"); if (aFileName.isEmpty()) return; // if (openDataAsStream(aFileName)) //保存为流数据文件 QMessageBox::information(this, "提示消息", "文件已经打开!");}void QGuiFileSys::actSaveStm_triggered(){ //以Qt预定义编码保存数据文件 QString curPath = QDir::currentPath(); QString aFileName = QFileDialog::getSaveFileName(this, tr("选择保存文件"), curPath, "Qt预定义编码数据文件(*.stm)"); if (aFileName.isEmpty()) return; // if (saveDataAsStream(aFileName)) //保存为流数据文件 QMessageBox::information(this, "提示消息", "文件已经成功保存!");}void QGuiFileSys::actOpenBin_triggered(){ //打开二进制文件 QString curPath = QDir::currentPath();//系统当前目录 QString aFileName = QFileDialog::getOpenFileName(this, tr("打开一个文件"), curPath, "二进制数据文件(*.dat)"); if (aFileName.isEmpty()) return; // if (openBinaryFile(aFileName)) //保存为流数据文件 QMessageBox::information(this, "提示消息", "文件已经打开!");}void QGuiFileSys::actSaveBin_triggered(){ //保存二进制文件 QString curPath = QDir::currentPath(); //调用打开文件对话框选择一个文件 QString aFileName = QFileDialog::getSaveFileName(this, tr("选择保存文件"), curPath, "二进制数据文件(*.dat)"); if (aFileName.isEmpty()) return; // if (saveBinaryFile(aFileName)) //保存为流数据文件 QMessageBox::information(this, "提示消息", "文件已经成功保存!");}void QGuiFileSys::actAppend_triggered(){ //添加行 QList aItemList; //容器类 QStandardItem* aItem; QString str; for (int i = 0; i < FixedColumnCount - 2; i++) { aItem = new QStandardItem("0"); //创建Item aItemList << aItem; //添加到容器 } aItem = new QStandardItem("优"); //创建Item aItemList << aItem; //添加到容器 str = theModel->headerData(theModel->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString(); aItem = new QStandardItem(str); //创建Item aItem->setCheckable(true); aItem->setEditable(false); aItemList << aItem; //添加到容器 theModel->insertRow(theModel->rowCount(), aItemList); //插入一行,需要每个Cell的Item QModelIndex curIndex = theModel->index(theModel->rowCount() - 1, 0);//创建最后一行的ModelIndex theSelection->clearSelection(); theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);}void QGuiFileSys::actInsert_triggered(){ //插入行 QList aItemList; //QStandardItem的容器类 QStandardItem* aItem; QString str; for (int i = 0; i < FixedColumnCount - 2; i++) { aItem = new QStandardItem("0"); //新建一个QStandardItem aItemList << aItem;//添加到容器类 } aItem = new QStandardItem("优"); //新建一个QStandardItem aItemList << aItem;//添加到容器类 str = theModel->headerData(theModel->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString(); aItem = new QStandardItem(str); //创建Item aItem->setCheckable(true); aItem->setEditable(false); aItemList << aItem;//添加到容器类 QModelIndex curIndex = theSelection->currentIndex(); theModel->insertRow(curIndex.row(), aItemList); theSelection->clearSelection(); theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);}void QGuiFileSys::actDelete_triggered(){ //删除行 QModelIndex curIndex = theSelection->currentIndex(); if (curIndex.row() == theModel->rowCount() - 1)//(curIndex.isValid()) theModel->removeRow(curIndex.row()); else { theModel->removeRow(curIndex.row()); theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select); }}void QGuiFileSys::actAlignLeft_triggered(){ if (!theSelection->hasSelection()) return; QModelIndexList selectedIndix = theSelection->selectedIndexes(); QModelIndex aIndex; QStandardItem* aItem; for (int i = 0; i < selectedIndix.count(); i++) { aIndex = selectedIndix.at(i); aItem = theModel->itemFromIndex(aIndex); aItem->setTextAlignment(Qt::AlignLeft); }}void QGuiFileSys::actAlignCenter_triggered(){ if (!theSelection->hasSelection()) return; QModelIndexList selectedIndix = theSelection->selectedIndexes(); QModelIndex aIndex; QStandardItem* aItem; for (int i = 0; i < selectedIndix.count(); i++) { aIndex = selectedIndix.at(i); aItem = theModel->itemFromIndex(aIndex); aItem->setTextAlignment(Qt::AlignHCenter); }}void QGuiFileSys::actAlignRight_triggered(){ if (!theSelection->hasSelection()) return; QModelIndexList selectedIndix = theSelection->selectedIndexes(); QModelIndex aIndex; QStandardItem* aItem; for (int i = 0; i < selectedIndix.count(); i++) { aIndex = selectedIndix.at(i); aItem = theModel->itemFromIndex(aIndex); aItem->setTextAlignment(Qt::AlignRight); }}void QGuiFileSys::actFontBold_triggered(bool checked){ if (!theSelection->hasSelection()) return; QModelIndexList selectedIndix = theSelection->selectedIndexes(); QModelIndex aIndex; QStandardItem* aItem; QFont font; for (int i = 0; i < selectedIndix.count(); i++) { aIndex = selectedIndix.at(i); aItem = theModel->itemFromIndex(aIndex); font = aItem->font(); font.setBold(checked); aItem->setFont(font); }}
效果如下
使用QDataStream保存文件时使用的数据编码的方式不同,可以保存为两种
1、使用Qt预定义编码保存各种类型数据的文件,定义文件后缀为“.stm”,Qt预定义编码指的是在写入某个数据类型到文件流时,使用Qt预定义的编码。使用Qt预定义编码保存的流文件,某些字节是QDataStream自己写入的,我们并不完全知道文件内每个字节的意义,但是使用QDataStream可以读出相应的数据
2、便准编码数据文件,定义文件后缀为“.dat”,在将数据写到文件时,完全使用数据的二进制原始内容,每个字节都有具体的定义,在读出数据时,只需要根据每个字节的定义读出数据即可