Qt 二进制文件的读写

Qt 二进制文件的读写

开发工具:VS2013 +QT5.8.0

实例功能概述

1、新建项目“sample7_2binFile”

完成以上步骤后,生成以下文件:

2、界面设计

如何添加资源文件:

鼠标双击“***.qrc”文件 弹出以下界面:

点击 “Add File” 找到要添加的 icons 所在位置,全选,“打开” 即添加上,别忘了点 “保存”

如何在工具栏添加QAction

现在菜单栏添加需要添加的QAction,然后把它拖放到工具栏上

可视化UI设计,完成后的界面如下图所示:

添加 Qt Class “QWComboBoxDelegate”

QWComboBoxDelegate.h 文件

#pragma once#include <QItemDelegate>class QWComboBoxDelegate : public QItemDelegate
{Q_OBJECTpublic:QWComboBoxDelegate(QObject *parent=0);~QWComboBoxDelegate();//自定义代理组件必须继承以下4个函数QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;};

QWComboBoxDelegate.cpp 文件

#include "QWComboBoxDelegate.h"#include    <QComboBox>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent): QItemDelegate(parent)
{
}QWComboBoxDelegate::~QWComboBoxDelegate()
{
}QWidget *QWComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(option);Q_UNUSED(index);QComboBox *editor = new QComboBox(parent);editor->addItem("优");editor->addItem("良");editor->addItem("一般");editor->addItem("不合格");return editor;
}void QWComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString str = index.model()->data(index, Qt::EditRole).toString();QComboBox *comboBox = static_cast<QComboBox*>(editor);comboBox->setCurrentText(str);
}void QWComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QComboBox *comboBox = static_cast<QComboBox*>(editor);QString str = comboBox->currentText();model->setData(index, str, Qt::EditRole);
}void QWComboBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(index);editor->setGeometry(option.rect);
}

添加 Qt Class “QWFloatSpinDelegate”

QWFloatSpinDelegate.h 文件

#pragma once#include <QStyledItemDelegate>class QWFloatSpinDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:QWFloatSpinDelegate(QObject *parent=0);~QWFloatSpinDelegate();//自定义代理组件必须继承以下4个函数//创建编辑组件QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;
};

QWFloatSpinDelegate.cpp 文件

#include "QWFloatSpinDelegate.h"#include  <QDoubleSpinBox>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")QWFloatSpinDelegate::QWFloatSpinDelegate(QObject *parent): QStyledItemDelegate(parent)
{
}QWFloatSpinDelegate::~QWFloatSpinDelegate()
{
}QWidget *QWFloatSpinDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(option);Q_UNUSED(index);QDoubleSpinBox *editor = new QDoubleSpinBox(parent);editor->setFrame(false);editor->setMinimum(0);editor->setDecimals(2);editor->setMaximum(10000);return editor;
}void QWFloatSpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{float value = index.model()->data(index, Qt::EditRole).toFloat();QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);spinBox->setValue(value);
}void QWFloatSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);spinBox->interpretText();float value = spinBox->value();QString str = QString::asprintf("%.2f", value);model->setData(index, str, Qt::EditRole);
}void QWFloatSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(index);editor->setGeometry(option.rect);
}

添加 Qt Class “QWIntSpinDelegate

QWIntSpinDelegate.h 文件

#pragma once#include <QStyledItemDelegate>class QWIntSpinDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:QWIntSpinDelegate(QObject *parent=0);~QWIntSpinDelegate();//自定义代理组件必须继承以下4个函数//创建编辑组件QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;//从数据模型获取数据,显示到代理组件中void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;//将代理组件的数据,保存到数据模型中void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;//更新代理编辑组件的大小void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,const QModelIndex &index) const Q_DECL_OVERRIDE;
};

QWIntSpinDelegate.cpp 文件

#include "QWIntSpinDelegate.h"#include    <QSpinBox>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")QWIntSpinDelegate::QWIntSpinDelegate(QObject *parent): QStyledItemDelegate(parent)
{
}QWIntSpinDelegate::~QWIntSpinDelegate()
{
}//创建代理编辑组件
QWidget *QWIntSpinDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option, const QModelIndex &index) const
{Q_UNUSED(option);Q_UNUSED(index);QSpinBox *editor = new QSpinBox(parent); //创建一个QSpinBoxeditor->setFrame(false); //设置为无边框editor->setMinimum(0);editor->setMaximum(10000);return editor;  //返回此编辑器
}//从数据模型获取数据,显示到代理组件中
void QWIntSpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{//获取数据模型的模型索引指向的单元的数据int value = index.model()->data(index, Qt::EditRole).toInt();QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  //强制类型转换spinBox->setValue(value); //设置编辑器的数值
}//将代理组件的数据,保存到数据模型中
void QWIntSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QSpinBox *spinBox = static_cast<QSpinBox*>(editor); //强制类型转换spinBox->interpretText(); //解释数据,如果数据被修改后,就触发信号int value = spinBox->value(); //获取spinBox的值model->setData(index, value, Qt::EditRole); //更新到数据模型
}//设置组件大小
void QWIntSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ Q_UNUSED(index);editor->setGeometry(option.rect);
}

sample7_2binFile.h 文件

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_sample7_2binFile.h"#include    <QLabel>
#include    <QStandardItemModel>
#include    <QItemSelectionModel>#include    "QWintspindelegate.h"
#include    "QWfloatspindelegate.h"
#include    "QWComboBoxDelegate.h"#define  FixedColumnCount   6  //文件固定6行class sample7_2binFile : public QMainWindow
{Q_OBJECTpublic:sample7_2binFile(QWidget *parent = Q_NULLPTR);private:Ui::sample7_2binFileClass ui;private://用于状态栏的信息显示QLabel  *LabCellPos; //当前单元格行列号QLabel  *LabCellText;//当前单元格内容QWIntSpinDelegate    intSpinDelegate;  //整型数QWFloatSpinDelegate  floatSpinDelegate;//浮点数QWComboBoxDelegate   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 on_currentChanged(const QModelIndex &current, const QModelIndex &previous);void on_actOpen_triggered();//打开文件void on_actAppend_triggered();//添加行void on_actInsert_triggered();//插入行void on_actDelete_triggered();//删除行void on_actSave_triggered();//保存文件void on_actAlignCenter_triggered();//居中对齐void on_actFontBold_triggered(bool checked);//字体粗体设置void on_actAlignLeft_triggered();//左对齐void on_actAlignRight_triggered();//右对齐void on_actTabReset_triggered();//表格复位void on_actSaveBin_triggered();//保存为自编码二进制文件void on_actOpenBin_triggered();//打开自编码二进制文件void on_actExit_triggered();//退出};

sample7_2binFile.cpp 文件

#include "sample7_2binFile.h"#include    <QFileDialog>
#include    <QDataStream>
#include    <QMessageBox>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")sample7_2binFile::sample7_2binFile(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);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(QModelIndex, QModelIndex)),this, SLOT(on_currentChanged(QModelIndex, 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); //表格复位setCentralWidget(ui.tabWidget); ////创建状态栏组件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);
}//表格复位,先删除所有行,再设置新的行数,表头不变
void sample7_2binFile::resetTable(int aRowCount)
{//QStringList     headerList;//headerList<<"测深(m)"<<"垂深(m)"<<"方位(°)"<<"总位移(m)"<<"固井质量"<<"测井取样";//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);//获取itemaItem->setCheckable(true);aItem->setData(str, Qt::DisplayRole);aItem->setEditable(false);//不可编辑}
}//将模型数据保存为Qt预定义编码的数据文件
bool sample7_2binFile::saveDataAsStream(QString &aFileName)
{QFile aFile(aFileName);  //以文件方式读出if (!(aFile.open(QIODevice::WriteOnly | QIODevice::Truncate)))return false;QDataStream aStream(&aFile);aStream.setVersion(QDataStream::Qt_5_8); //设置版本号,写入和读取的版本号要兼容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;// 写入文件流,qint16aItem = theModel->item(i, 1); //垂深qreal chuiShen = aItem->data(Qt::DisplayRole).toFloat();aStream << chuiShen;//写入文件流, qrealaItem = theModel->item(i, 2); //方位qreal fangWei = aItem->data(Qt::DisplayRole).toFloat();aStream << fangWei;//写入文件流, qrealaItem = theModel->item(i, 3); //位移qreal weiYi = aItem->data(Qt::DisplayRole).toFloat();aStream << weiYi;//写入文件流, qrealaItem = 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;
}//从Qt预定义流文件读入数据
bool sample7_2binFile::openDataAsStream(QString &aFileName)
{QFile aFile(aFileName);  //以文件方式读出if (!(aFile.open(QIODevice::ReadOnly)))return false;QDataStream aStream(&aFile); //用文本流读取文件aStream.setVersion(QDataStream::Qt_5_8); //设置流文件版本号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;//读取测深, qint16index = theModel->index(i, 0);aItem = theModel->itemFromIndex(index);aItem->setData(ceShen, Qt::DisplayRole);aStream >> chuiShen;//垂深,qrealindex = theModel->index(i, 1);aItem = theModel->itemFromIndex(index);aItem->setData(chuiShen, Qt::DisplayRole);aStream >> fangWei;//方位,qrealindex = theModel->index(i, 2);aItem = theModel->itemFromIndex(index);aItem->setData(fangWei, Qt::DisplayRole);aStream >> weiYi;//位移,qrealindex = theModel->index(i, 3);aItem = theModel->itemFromIndex(index);aItem->setData(weiYi, Qt::DisplayRole);aStream >> zhiLiang;//固井质量,QStringindex = theModel->index(i, 4);aItem = theModel->itemFromIndex(index);aItem->setData(zhiLiang, Qt::DisplayRole);aStream >> quYang;//boolindex = theModel->index(i, 5);aItem = theModel->itemFromIndex(index);if (quYang)aItem->setCheckState(Qt::Checked);elseaItem->setCheckState(Qt::Unchecked);}aFile.close();return true;
}//保存为纯二进制文件
bool sample7_2binFile::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::LittleEndianqint16  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); //获取表头itemQString str = aItem->text(); //获取表头文字btArray = str.toUtf8(); //转换为字符数组aStream.writeBytes(btArray, btArray.length()); //写入文件流,长度uint型,然后是字符串内容}//获取数据区文字,qint8   yes = 1, no = 0; //分别代表逻辑值 true和falsefor (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 falseif (quYang)aStream.writeRawData((char *)&yes, sizeof(qint8));elseaStream.writeRawData((char *)&no, sizeof(qint8));}aFile.close();return true;
}//打开二进制文件
bool sample7_2binFile::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;  //也就是 quint32for (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和falseQModelIndex 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);elseaItem->setCheckState(Qt::Unchecked);}aFile.close();return true;
}void sample7_2binFile::on_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); //从模型索引获得Itemthis->LabCellText->setText("单元格内容:" + aItem->text());QFont   font = aItem->font();//ui.actFontBold->setChecked(font.bold());}
}void sample7_2binFile::on_actOpen_triggered()
{QString curPath = QDir::currentPath();//调用打开文件对话框打开一个文件QString aFileName = QFileDialog::getOpenFileName(this, tr("打开一个文件"), curPath,"流数据文件(*.stm)");if (aFileName.isEmpty())return; //if (openDataAsStream(aFileName)) //保存为流数据文件QMessageBox::information(this, "提示消息", "文件已经打开!");
}//添加行
void sample7_2binFile::on_actAppend_triggered()
{QList<QStandardItem*>    aItemList; //容器类QStandardItem   *aItem;QString str;for (int i = 0; i < FixedColumnCount - 2; i++){aItem = new QStandardItem("0"); //创建ItemaItemList << aItem;   //添加到容器}aItem = new QStandardItem("优"); //创建ItemaItemList << aItem;   //添加到容器str = theModel->headerData(theModel->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString();aItem = new QStandardItem(str); //创建ItemaItem->setCheckable(true);aItem->setEditable(false);aItemList << aItem;   //添加到容器theModel->insertRow(theModel->rowCount(), aItemList); //插入一行,需要每个Cell的ItemQModelIndex curIndex = theModel->index(theModel->rowCount() - 1, 0);//创建最后一行的ModelIndextheSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}//插入行
void sample7_2binFile::on_actInsert_triggered()
{QList<QStandardItem*>    aItemList;  //QStandardItem的容器类QStandardItem   *aItem;QString str;for (int i = 0; i < FixedColumnCount - 2; i++){aItem = new QStandardItem("0"); //新建一个QStandardItemaItemList << aItem;//添加到容器类}aItem = new QStandardItem("优"); //新建一个QStandardItemaItemList << aItem;//添加到容器类str = theModel->headerData(theModel->columnCount() - 1, Qt::Horizontal, Qt::DisplayRole).toString();aItem = new QStandardItem(str); //创建ItemaItem->setCheckable(true);aItem->setEditable(false);aItemList << aItem;//添加到容器类QModelIndex curIndex = theSelection->currentIndex();theModel->insertRow(curIndex.row(), aItemList);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}//删除行
void sample7_2binFile::on_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);}
}//以Qt预定义编码保存数据文件
void sample7_2binFile::on_actSave_triggered()
{ QString curPath = QDir::currentPath();QString aFileName = QFileDialog::getSaveFileName(this, tr("选择保存文件"), curPath,"Qt预定义编码数据文件(*.stm)");if (aFileName.isEmpty())return; //if (saveDataAsStream(aFileName)) //保存为流数据文件QMessageBox::information(this, "提示消息", "文件已经成功保存!");
}void sample7_2binFile::on_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 sample7_2binFile::on_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);}}void sample7_2binFile::on_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 sample7_2binFile::on_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 sample7_2binFile::on_actTabReset_triggered()
{resetTable(10);
}//保存二进制文件
void sample7_2binFile::on_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 sample7_2binFile::on_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 sample7_2binFile::on_actExit_triggered()
{this->close();
}

main.ccp 文件

#include "sample7_2binFile.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);sample7_2binFile w;w.show();return a.exec();
}

运行结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/455644.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【AI视频抠图整合包及教程】开启视觉分割新纪元 —— Meta SAM 2

在数字化时代&#xff0c;Meta公司推出的SAM 2&#xff08;Segment Anything Model 2&#xff09;标志着图像和视频分割技术的一个新高度。SAM 2不仅继承了前代SAM模型的卓越性能&#xff0c;更在实时处理、视频分割、交互式提示等方面实现了重大突破。以下是SAM 2的全面营销文…

075_基于springboot的万里学院摄影社团管理系统

目录 系统展示 开发背景 代码实现 项目案例 获取源码 博主介绍&#xff1a;CodeMentor毕业设计领航者、全网关注者30W群落&#xff0c;InfoQ特邀专栏作家、技术博客领航者、InfoQ新星培育计划导师、Web开发领域杰出贡献者&#xff0c;博客领航之星、开发者头条/腾讯云/AW…

502 错误码通常出现在什么场景?

服务器过载场景 高流量访问&#xff1a;当网站遇到突发的高流量情况&#xff0c;如热门产品促销活动、新闻热点事件导致网站访问量激增时&#xff0c;服务器可能会因承受过多请求而无法及时响应。例如&#xff0c;电商平台在 “双十一” 等购物节期间&#xff0c;大量用户同时…

[分享] SQL在线编辑工具(好用)

在线SQL编写工具&#xff08;无广告&#xff09; - 在线SQL编写工具 - Web SQL - SQL在线编辑格式化 - WGCLOUD

AI修图太牛了! | 换模特、换服装、换背景都如此简单!

前言 推荐一款我最近发现的AI工具&#xff0c;它就是最懂电商的千鹿AI&#xff0c;专门用来做电商产品图、场景图的&#xff0c;除此外还有AI修图、线稿上色、批量抠图等等超多图片处理工具。 本人也从事过电商行业&#xff0c;包括跨境电商&#xff0c;非常知道电商人的疾苦…

Java 多线程(七)—— 定时器

定时器介绍与使用 先简单介绍一下什么是定时器&#xff1a;定时器类似生活中的闹钟&#xff0c;当时间一到&#xff0c;我们就会去做某些事情。 在代码层面理解就是&#xff0c;当我们设置的时间一到&#xff0c;程序就会执行我们固定的代码片段&#xff08;也就是任务&#x…

谷歌新安装包文件形式 .aab 在UE4中的打包原理

摘要 本文学习了aab的基本概念以及UE4中产生aab的构建原理。 从官网了解基本概念 官网&#xff1a;Android Developers 1、什么是aab&#xff1f; .aab包形如&#xff1a; 2021年7月&#xff0c;在Google Play应用程序中&#xff0c;已经有数千个应用程序率先跟进了AAB格式。…

OpenCV视觉分析之运动分析(2)背景减除类:BackgroundSubtractorKNN的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 K-最近邻&#xff08;K-nearest neighbours, KNN&#xff09;基于的背景/前景分割算法。 该类实现了如 319中所述的 K-最近邻背景减除。如果前景…

Zypher Network Layer3 主网上线,“宝藏方舟”活动是亮点

前言 随着 Zytron Layer3 主网的上线&#xff0c;Zypher Network联合Linea共同推出了“宝藏方舟”活动&#xff0c;用户可通过参与活动&#xff0c;获得包括代币、积分、SBT等系列奖励。 Zypher Network 是一个以ZK方案为核心的游戏底层堆栈&#xff0c;其提供了一个具备主权…

C++20中头文件span的使用

<span>是C20中新增加的头文件&#xff0c;此头文件是containers库的一部分。包括&#xff1a; 1.模板类std::span&#xff1a;连续对象序列的非拥有视图(view)。std::span可以具有static extent&#xff0c;在这种情况下&#xff0c;序列中的元素数量在编译时已知并以typ…

探寻医疗行业人力资源管理系统优选方案

医疗机构管理日益重要&#xff0c;ZohoPeople HRMS助力医疗行业优化人力管理&#xff0c;提升效率。其涵盖智能排班、培训发展、合规保障、绩效管理等功能&#xff0c;支持全球化及远程协作&#xff0c;是医疗行业人力资源管理的有效工具。 一、医疗行业人力资源管理的复杂性 …

基于SpringBoot+Vue+uniapp微信小程序的社区门诊管理系统的详细设计和实现(源码+lw+部署文档+讲解等)

项目运行截图 技术框架 后端采用SpringBoot框架 Spring Boot 是一个用于快速开发基于 Spring 框架的应用程序的开源框架。它采用约定大于配置的理念&#xff0c;提供了一套默认的配置&#xff0c;让开发者可以更专注于业务逻辑而不是配置文件。Spring Boot 通过自动化配置和约…

15.正则化——防止过拟合的有效手段

引言 在人工智能(AI)领域&#xff0c;尤其是在机器学习和深度学习中&#xff0c;正则化(regularization)具有非常重要的地位。它不仅是训练模型过程中不可或缺的一部分&#xff0c;也是提高模型性能的关键因素之一。此外&#xff0c;正则化还可以提升模型的泛化能力&#xff0…

产品如何实现3D展示?具体步骤如下

产品实现3D展示主要依赖于先进的3D建模与展示技术。以下是产品实现3D展示的具体步骤和方法&#xff1a; 一、3D建模 使用专业的3D建模软件&#xff0c;如Blender、Maya、3ds Max等&#xff0c;这些软件提供了丰富的建模工具和材质编辑器&#xff0c;能够创建出高精度的3D模型…

Flutter 12 实现双击屏幕显示点赞爱心多种动画(AnimationIcon)效果

本文主要是使用Flutter封装一个双击屏幕显示点赞爱心UI效果&#xff0c;并实现了爱心Icon 透明度、缩放、旋转、渐变等动画效果。 实现效果&#xff1a; 实现逻辑&#xff1a; 1、封装FavoriteGesture&#xff08;爱心手势&#xff09;实现双击屏幕显示爱心Icon&#xff1b; …

文件摆渡系统选型指南:如何找到最适合您的数据安全解决方案?

在当今数字化时代&#xff0c;数据的安全传输与共享已成为企业运营中不可或缺的一环。文件摆渡系统&#xff0c;作为实现数据在不同安全域之间高效、安全传输的重要工具&#xff0c;其选型直接关系到企业数据的安全性与业务效率。本文将为您详细介绍如何挑选最适合您企业的文件…

视频网站系统的设计与实现(论文+源码)_kaic

毕 业 设 计&#xff08;论 文&#xff09; 题目&#xff1a;视频网站系统 摘 要 使用旧方法对视频信息进行系统化管理已经不再让人们信赖了&#xff0c;把现在的网络信息技术运用在视频信息的管理上面可以解决许多信息管理上面的难题&#xff0c;比如处理数据时间很长&#…

为什么k8s不支持docker-kubernetes

为什么Kubernetes不再支持Docker&#xff1f; 在Kubernetes 1.20版本之后&#xff0c;Kubernetes宣布逐步停止对Docker作为容器运行时的支持。这一改变在容器管理领域引起了广泛关注。许多人不禁疑惑&#xff1a;Kubernetes与Docker一向密切合作&#xff0c;为何会做出这样的决…

骨传导耳机哪个品牌好?2024年五大热门精选骨传导耳机推荐

在当今快节奏的生活中&#xff0c;人们对于个人音频设备的需求不仅限于优质的音质体验&#xff0c;还越来越注重健康与舒适。骨传导耳机作为一种新兴的技术产品&#xff0c;以其独特的听觉传递方式——通过颞骨而非耳道传递声音——赢得了众多用户的青睐。这种技术不仅可以提供…

Webserver(1)Linux开发环境搭建

目录 配置软件虚拟机中安装ubuntu安装ubuntu18的操作系统 安装VM tools安装XshellVscode远程连接到虚拟机 配置软件 VMwareVScodeg安装ubuntu 18.04.iso 或者镜像版本 XShellXFTP 虚拟机中安装ubuntu 安装ubuntu18的操作系统 开启虚拟机 选择中文简体 安装VM tools 打开v…