qfile 创建文件_Qt之二进制文件读写

本文介绍了如何使用Qt的QFile和QDataStream进行二进制文件的读写操作。QFile负责文件的物理交互,而QDataStream则以数据流方式处理内容。内容包括使用Qt预定义编码保存为“.stm”文件和标准编码的“.dat”文件,解释了两种编码方式的区别和在读写过程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击上方“Qt学视觉”,选择“星标”公众号重磅干货,第一时间送达

想要学习的同学们还请认真阅读每篇文章,相信你一定会有所收获

除了文本文件之外,其他需要按照一定的格式定义读写的文件都称为二进制文件,每种格式的二进制文件都有自己的格式定义,写入数据时按照一定的顺序写入,读出时也按照相应的顺序读出

Qt使用QFile和QDataStream进行二进制数据文件的读写,QFile负责文件的IO设备接口,即与文件的物理交互,QDataStream以数据流的方式读取文件内容或写入文件内容

紧接着上一节的界面往上加这节的界面

8af8c38d46b6298ceae33d7e6d0f5658.png

fa9a99fedd930ccc096a8d81587cbaf4.png

头文件

#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); //浮点数    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值