点击上方“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); //浮点数