07-2_Qt 5.9 C++开发指南_二进制文件读写(stm和dat格式)

文章目录

  • 1. 实例功能概述
  • 2. Qt预定义编码文件的读写
    • 2.1 保存为stm文件
    • 2.2 stm文件格式
    • 2.3 读取stm文件
  • 3. 标准编码文件的读写
    • 3.1 保存为dat文件
    • 3.2 dat文件格式
    • 3.3 读取dat文件
  • 4. 框架及源码
    • 4.1 可视化UI设计
    • 4.2 mainwindow.cpp

1. 实例功能概述

除了文本文件之外,其他需要按照一定的格式定义读写的文件都称为二进制文件。每种格式的二进制文件都有自己的格式定义,写入数据时按照一定的顺序写入,读出时也按照相应的顺序读出。例如地球物理中常用的 SEG-Y 格式文件,必须按照其标准格式要求写入数据才符合这种文件的格式规范,读取数据时也需要按照格式定义来读出。
Qt 使用 QFile 和QDataStream 进行二进制数据文件的读写。QFile 负责文件的10 设备接口,即与文件的物理交互,QDataStream 以数据流的方式读取文件内容或写入文件内容。

本节以实例 samp7_2 演示二进制文件的读写,图7-2 是程序运行的界面。

在这里插入图片描述

实例以表格形式编辑一个数据表,采用 Model/View 结构,编辑后的数据保存为二进制文件,这与第 5.4 节的实例用纯文本文件存储数据不同。

根据 QDataStream 保存文件时使用的数据编码的方式不同,可以保存为两种文件。

(1)用Qt预定义编码保存各种类型数据的文件,定义文件后缀为“.stm”。Qt 预定义编码是指在写入某个类型数据,如整形数、字符串等到文件流时,使用 Qt 预定义的编码。可以将这种 Qt 预定义数据格式编码类比于 HTML 的标记符,Qt 写入某种类型数据时用了 Qt 预定义的标记符,读出数据时,根据标记符读出数据。使用 Qt 预定义编码保存的流文件,某些字节是 QDataStream 自己写入的,我们并不完全知道文件内每个字节的意义,但是用 QDataStream 可以读出相应的数据。

(2)标准编码数据文件,定义文件后缀为“.dat”。在将数据写到文件时,完全使用数据的二进制原始内容,每个字节都有具体的定义,在读出数据时,只需根据每个字节的定义读出数据即可。

实例samp7_2具有如下功能:

  • 可以在表格内编辑数据,同样的表格数据内容可以保存为两种格式的文件,Qt 预定义编码文件(stm 文件)和标准编码文件 (dat 文件);

  • 界面上的表格数据可以修改,可以添加行、插入行、删除行;

  • 可以读取 stm 文件或 dat 文件,虽然文件格式不一样,但对相同的界面数据表存储的文件的实质内容是一样的。

实例samp7_2的主窗口使用了 Model/View 结构、标准项数据模型 QStandardItemModel和选择模型QItemSelectionModel,界面上使用了QTableView 组件,还有代理组件。这些涉及 Model/View的设计可参考第 5.4 节和 5.5 节,这些设计在前述章节里已经介绍过,不是本节的重点,不再详述。

为便于理解后面的程序,这里给出主窗口 MainWindow 类中自定义的一些变量和函数,具体如下(忽略了自动生成的一些定义):

//用于状态栏的信息显示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);//打开二进制文件

2. Qt预定义编码文件的读写

2.1 保存为stm文件

先看文件保存功能,因为从文件保存功能的代码可以看出文件内数据的存储顺序。在图 7-2的窗口上编辑表格的数据后,单击工具栏上的“保存 st 文件”,可以使用 Qt 预定义编码方式保存文件。此按钮的响应代码如下:

void MainWindow::on_actSave_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,"提示消息","文件已经成功保存!");
}bool MainWindow::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;// 写入文件流,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;
}

自定义函数 saveDataAsStream()将表格的数据模型 theModel 的数据保存为一个 stm 文件。代码首先是创建 QFile 对象aFile 打开文件,然后创建 DataStream 对象aStream 与 QFile 对象关联。

在开始写数据流之前,为QDataStream 对象 aStream 设置版本号,即调用 setVersion()函数并传递一个QDataStream::Version 枚举类型的值。

aStream.setVersion(QDataStream::Qt_5_9); //设置版本号,写入和读取的版本号要兼容

这表示aStream将以QDataStream::Qt_5_9版本的预定义类型写文件流

注意:以 Qt 的预定义类型编码保存的文件需要指定流版本号,因为每个版本的 Qt 对数据类型的编码可能有差别,需要保证写文件和读文件的流版本是兼容的。

接下来,就是按照需要保存数据的顺序写入文件流。例如在文件开始,先写入行数和列数两个 qint16 的整数。因为行数和列数关系到后面的数据是如何组织的,因此在读取文件数据时,首先读取这两个整数,然后根据数据存储方式的约定,就知道后续数据该如何读取了。向文件写入数据时,直接用流的输入操作,如:

    aStream>>rowCount; //读取行数aStream>>colCount; //列数

在读取各列的表头字符串之后,将其写入数据流。然后逐行扫描表格的数据模型,将每一行的列数据写入数据流。
数据流写入数据时都使用运算符“<<”,不论写的是 qint16、qreal,还是字符串。除了可以写入基本的数据类型外,QDataStream 流操作还可以写入很多其他类型的数据,如QBrush、QColor、QImage、QIcon 等,这些称为可序列化的数据类型(Serializing Qt Data Types)
QDataStream 以流操作写入这些数据时,我们并不知道文件里每个字节是如何存储的,但是知道数据写入的顺序,以及每次写入数据的类型。在文件数据读出时,只需按照顺序和类型对应读出即可。

2.2 stm文件格式

根据 saveDataAsStream()函数的代码,可知 Qt 预定义编码保存的 stm 文件的格式,如表 7-1所示。

在这里插入图片描述
在这里插入图片描述

从表 7-1 中可以知道 stm 文件的数据存储顺序和类型,但是并不知道 qit16 类型的数据存储为几个字节以及 QString 类型的数据是如何定义长度和字符内容的,其实也不需要知道这些具体的存储方式,在从文件读出时,只需按照表 7-1 的顺序和类型读出数据即可。

2.3 读取stm文件

下面是工具栏按钮“打开 stm 文件”的响应代码及相关函数代码,选择需要打开的 stm 文件后,主要是调用自定义函数 openDataAsStream()将其打开。

void MainWindow::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,"提示消息","文件已经打开!");
}bool MainWindow::openDataAsStream(QString &aFileName)
{ //从Qt预定义流文件读入数据QFile aFile(aFileName);  //以文件方式读出if (!(aFile.open(QIODevice::ReadOnly)))return false;QDataStream aStream(&aFile); //用文本流读取文件aStream.setVersion(QDataStream::Qt_5_9); //设置流文件版本号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;
}

读取 stm 文件的数据之前也必须设置 QDataStream 的流版本号,应该等于或高于数据保存时的流版本号。

然后就是按照表 7-1 所示的写入数据时的顺序和类型,相应地读出每个数据。文件里最早的两个数据是表格的行数和列数,读出这两个数据,就能知道数据的行数和列数,并调用自定义函数 resetTable()给数据模型复位,并设置其行数。
然后将保存的每行数据读入到数据模型的每个项中,这样窗口上的 QTableView 组件就可以显示数据了。

使用QDataStream 的流操作方式读写文件的特点如下。

  • 读写操作都比较方便,支持读写各种数据类型,包括 Qt 的一些类,还可以为流数据读写扩展自定义的数据类型。读写某种类型的数据时,只要是流支持即可,而在文件内部是如何存储的,用户无需关心,由 Qt 预定义。

  • 写文件和读文件时必须保证使用的流版本兼容,即流的版本号相同,或读取文件的流版本号高于写文件时的流版本号。这是因为在不同的流版本中,流支持的数据类型的读写方式可能有所改变,必须保证读写版本的兼容。

  • 用这种方式保存文件时,写入数据采用 Qt 预定义的编码,即写入文件的二进制编码是由Qt预定义的,写多少个字节、字节是什么样的顺序,用户是不知道的。如果是由QDataStream读取数据,只需按类型读出即可。但是,如果由这种方法创建的文件是用于交换的,需要用其他的编程语言(如 Matlab) 来读取文件内容,则存在问题了。因为其他语言并没有与Qt 的流写入完全一致的流读出功能,例如,其他语言并不知道 Qt 保存的 QString 或 QFont的内容是如何组织的。

3. 标准编码文件的读写

3.1 保存为dat文件

前面是采用 Qt 预定义编码读写 stm 文件,这种方法使用简单,但是文件的格式不完全透明,不能创建用于交换的通用格式文件。
创建通用格式文件(即文件格式完全透明,每个字节都有具体的定义,如 SEG-Y 文件)的方法是以标准编码方式创建文件,使文件的每个字节都有具体的定义。用户在读取这种文件时,按照文件格式定义读取出每个字节数据并做解析即可,不管使用什么编程语言都可以编写读写文件的程序
主窗口工具栏上的“保存 dat 文件”按将表格中的数据保存为标准编码的文件,文件后缀是“.dat”。保存 dat 文件的代码是:

void MainWindow::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,"提示消息","文件已经成功保存!");
}bool MainWindow::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;
}
  • 字节序

在保存为标准编码的二进制文件时,无须指定 QDataStream 的版本,因为不会用到 Qt的类型预定义编码,文件的每个字节的意义都是用户自己定义的。但是如有必要,需要为文件指定字节顺序,如:

aStream.setByteOrder(QDataStream::LittleEndian);//windows平台

字节顺序分为大端字节序和小端字节序,小端字节序指低字节数据存放在内存低地址处,高字节数据存放在内存高地址处;大端字节序则相反。

基于X86平台的计算机是小端字节序的,所以 Windows 系统是小端字节序,而有的嵌入式平台或工作站平台则是大端字节序的。读取一个文件时,首先需要知道它是以什么字节序存储的这样才可以正确的读出。

setByteOrder()函数的参数是 QDataStream::ByteOrder 枚举类型常量,QDataStream::BigEndian
是大端字节序,QDataStream::LittleEndian 是小端字节序。

  • writeRawData()函数

QdataStream 采用函数 writeRawData()将数据写入数据流,在保存qint8、qint16、qreal等类型的数据时都使用这个函数,其函数原型是:

int QDataStream::writeRawData(const char *s, int len)

其中参数s是一个指向字节型数据的指针,len 是字节数据的长度。调用 writeRawData()函数将会向文件流连续写入len 个字节的数据,这些字节数据保存在指针 s 指向的起始地址里。例如,将qint16类型变量rowCount 写入文件的语句是:

    qint16  rowCount=theModel->rowCount();qint16  colCount=theModel->columnCount();
  • writeBytes()函数

在将字符串数据写入文件时,使用的是 writeBytes()函数,而不是 writeRawData()。下面是writeBytes()函数的原型定义:

QDataStream &QDataStream::writeBytes(const char *s, uint len)

其中参数s 是一个指向字节型数据的指针,len 是字节数据的长度。writeBytes()在写入数据时,会先将 len 作为一个 quint32 类型写入数据流,然后再写入 len 个从指针s 获取的数据。

writeBytes()适合于写入字符串数据,因为在写入字符串之前要先写入字符串的长度,这样在读取文件时,就能知道字符串的长度,以便正确读出字符串。

例如,下面的代码将字符串“Depth”写入文件流:

QString str="Depth”;
QByteArray btArray=str.toUtf8();
aStream.writeBytes(btArray,btArray.length());

文件中实际保存的内容见表 7-2。前 4 个字节是 quint32 类型的整数,表示保存数据的字节个数,这里是 5,表示后续有5 个字节数据。从第 5 字节开始,是保存的字符串”Depth”的每个字符的ASCII码。
在这里插入图片描述
由于写入文件的字符串的长度一般是不固定的,因此如果以 writeRawData()函数写入文件,只会写入字符串的内容,而没有表示字符串的长度。在文件读出时,如果不已知字符串长度,则难以正确读出字符串内容。而 writeBytes()函数首先写入了字符串的长度,在读取文件时,先从前四个字节读出字符串长度,知道数据有多少个字节就可以正确读出了。

QDataStream 提供了与 writeBytes()对应的函数 readBytes(),它可以自动读取长度和内容,适用于字符串数据的读取。

3.2 dat文件格式

用 saveBinaryFile()函数保存数据为标准编码二进制文件,文件后缀为“.dat”。根据saveBinaryFile()函数的内容,dat 文件的格式见表 7-3。

在这里插入图片描述

在表 7-3 中,可以看到文件内的每个字节都是有具体定义的,这样,无论用什么语言编写一个文件读取的程序,只要按照这个格式来读取,都可以正确读出文件内容。

dat 文件的数据是否是按照表 7-3 所示的顺序存储的呢?可以创建一个简单的数据表格,保存为 dat 后缀的文件,然后用显示文件二进制内容的软件来查看,如 ltraEdit 或 WinHex,这些软件在分析文件格式,编写文件读写程序时特别有用。

3.3 读取dat文件

对于保存的 dat 文件,主窗口工具栏上的“打开 dat 文件”按钮可以打开保存的 dat 文件,下面是打开 dat 文件的函数 openBinaryFile()的代码。

bool MainWindow::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;
}
  • 字节序

在流创建后,需要用 setByteOrder()函数指定字节序,并且与写入文件时用的字节序一致。

  • readRawData()函数

在读取基本类型数据时,使用QDataStream 的readRawData()函数,该函数原型为:

int QDataStream::readRawData(char *s, int len)

它会读取 len 个字节的数据,并且保存到指针 s 指向的存储区。例如:

    qint16  rowCount,colCount;aStream.readRawData((char *)&rowCount, sizeof(qint16));aStream.readRawData((char *)&colCount, sizeof(qint16));
  • readBytes()函数

读取字符串时使用readBytes()函数,它是与writeBytes()功能对应的函数,其函数原型为:

QDataStream &QDataStream::readBytes(char *&s, uint &l)

对应表格 7-2,使用readBytes()函数时,会先自动读取前 4 个字节数据作为quint32 的数据并赋值给 len 参数,因为 len 是以引用方式传递的参数,所以,len 返回读取的数据的字节数。然后根据 len 的大小读取相应字节的数据,存储到指针 s 指向的存储区。

4. 框架及源码

4.1 可视化UI设计

在这里插入图片描述

4.2 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"#include    <QFileDialog>
#include    <QDataStream>
#include    <QMessageBox>void MainWindow::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); //不可编辑}
}bool MainWindow::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;// 写入文件流,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;
}bool MainWindow::openDataAsStream(QString &aFileName)
{ //从Qt预定义流文件读入数据QFile aFile(aFileName);  //以文件方式读出if (!(aFile.open(QIODevice::ReadOnly)))return false;QDataStream aStream(&aFile); //用文本流读取文件aStream.setVersion(QDataStream::Qt_5_9); //设置流文件版本号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 MainWindow::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 MainWindow::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;
}MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{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);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::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 MainWindow::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 MainWindow::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 MainWindow::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 MainWindow::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);}
}void MainWindow::on_actSave_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 MainWindow::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 MainWindow::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 MainWindow::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 MainWindow::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 MainWindow::on_actTabReset_triggered()
{//表格复位resetTable(10);
}void MainWindow::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 MainWindow::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,"提示消息","文件已经打开!");
}

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

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

相关文章

pve和openwrt以及我的电脑中网络的关系和互通组网

情况1 一台主机 有4个口&#xff0c;分别eth0,eth1,eth2,eth3 pve有管理口 这个情况下 &#xff0c;没有openwrt 直接电脑和pve管理口连在一起就能进pve管理界面 情况2 假设pve 的管理口味eth0 openwrt中桥接的是eth0 eth1 eth2 那么电脑连接eth3或者pve管理口设置eth3&#xf…

MySQL库的操作

文章目录 MySQL库的操作1. 创建数据库2. 字符集和校验规则(1) 查看系统默认字符集以及校验规则(2) 查看数据库支持的字符集和校验规则(3) 案例(4) 校验规则对数据库的影响 3. 查看数据库4. 修改数据库5. 删除数据库6. 数据库的备份和恢复(1) 备份(2) 还原 7. 查看连接情况 MySQ…

在 Windows 中恢复数据的 5 种方法

发生数据丢失的原因有多种。无论是因为文件被意外删除、文件系统或操作系统损坏&#xff0c;还是由于软件或硬件级别的存储故障&#xff0c;数据都会在您最意想不到的时候丢失。今天我们重点介绍五种数据恢复方法&#xff0c;以应对意外情况的发生。 1.从另一台机器启动硬盘 如…

分享一组天气组件

先看效果&#xff1a; CSS部分代码&#xff08;查看更多&#xff09;&#xff1a; <style>:root {--bg-color: #E9F5FA;--day-text-color: #4DB0D3;/* 多云 */--cloudy-background: #4DB0D3;--cloudy-temperature: #E6DF95;--cloudy-content: #D3EBF4;/* 晴 */--sunny-b…

Https、CA证书、数字签名

Https Http协议 Http协议是目前应用比较多应用层协议&#xff0c;浏览器对于Http协议已经实现。Http协议基本的构成部分有 请求行 &#xff1a; 请求报文的第一行请求头 &#xff1a; 从第二行开始为请求头内容的开始部分。每一个请求头都是由K-V键值对组成。请求体&#xf…

【C++入门到精通】C++入门 —— vector (STL)

阅读导航 前言一、vector简介1. 概念2. 特点 二、vector的使用1.vector 构造函数2. vector 空间增长问题⭕resize 和 reserve 函数 3. vector 增删查改⭕operator[] 函数 三、迭代器失效温馨提示 前言 前面我们讲了C语言的基础知识&#xff0c;也了解了一些数据结构&#xff0…

软件测试基础篇——Docker

1、docker技术概述 docker描述&#xff1a;docker是一项虚拟化的容器技术&#xff08;类似于虚拟机&#xff09;&#xff0c;docker技术给使用者提供一个平台&#xff0c;在该平台上可以利用提供的容器&#xff0c;对每一个应用程序进行单独的封装隔离&#xff0c;每一个应用程…

计算机竞赛 opencv python 深度学习垃圾图像分类系统

0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; opencv python 深度学习垃圾分类系统 &#x1f947;学长这里给一个题目综合评分(每项满分5分) 难度系数&#xff1a;3分工作量&#xff1a;3分创新点&#xff1a;4分 这是一个较为新颖的竞…

Monkey测试真的靠谱吗?

Monkey测试&#xff0c;顾名思义&#xff0c;就是模拟一只猴子在键盘上乱敲&#xff0c;从而达到测试被测系统的稳定性。Monkey测试&#xff0c;是Android自动化测试的一种手段&#xff0c;Monkey测试本身非常简单&#xff0c;Android SDK 工具支持adb Shell命令&#xff0c;实…

208、仿真-51单片机脉搏心率与心电报警Proteus仿真设计(程序+Proteus仿真+配套资料等)

毕设帮助、开题指导、技术解答(有偿)见文未 目录 一、硬件设计 二、设计功能 三、Proteus仿真图 四、程序源码 资料包括&#xff1a; 需要完整的资料可以点击下面的名片加下我&#xff0c;找我要资源压缩包的百度网盘下载地址及提取码。 方案选择 单片机的选择 方案一&a…

ElasticSearch 7.4学习记录(基础概念和基础操作)

若你之前从未了解过ES&#xff0c;本文将由浅入深的一步步带你理解ES&#xff0c;简单使用ES。作者本人就是此状态&#xff0c;通过学习和梳理&#xff0c;产出本文&#xff0c;已对ES有个全面的了解和想法&#xff0c;不仅将知识点梳理&#xff0c;也涉及到自己的理解&#xf…

行业追踪,2023-08-09

自动复盘 2023-08-09 凡所有相&#xff0c;皆是虚妄。若见诸相非相&#xff0c;即见如来。 k 线图是最好的老师&#xff0c;每天持续发布板块的rps排名&#xff0c;追踪板块&#xff0c;板块来开仓&#xff0c;板块去清仓&#xff0c;丢弃自以为是的想法&#xff0c;板块去留让…

linux学习——Redis基础

目录 一、noSQL 类型 特点及应用场景 二、Redis 三、安装方式 编译安装 rpm安装 四、目录结构 /etc/redis.conf 五、Redis命令 六、本地登录和远程登录 本地登录 远程登录 七、数据库操作 帮助信息 库操作 数据操作 八、Redis持久化 一、RDB类型 二、AOF模式 一…

伪类和伪元素有何区别?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 伪类&#xff08;Pseudo-class&#xff09;⭐ 伪元素&#xff08;Pseudo-element&#xff09;⭐ 区别总结⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前…

信号调制原理演示,模拟和数字调制技术大比拼

【中英双语字幕】信号调制原理演示&#xff0c;模拟和数字调制技术大比拼&#xff01;_哔哩哔哩_bilibili

腾讯云轻量应用服务器Typecho应用模板搭建博客流程

腾讯云百科分享使用腾讯云轻量应用服务器Typecho应用模板搭建博客流程&#xff0c;Typecho 是开源的博客建站平台&#xff0c;具有轻量、高效、稳定等特点&#xff0c;操作界面简洁友好。该镜像基于 CentOS 7.6 64 位操作系统&#xff0c;并已预置 Nginx、PHP、MariaDB 软件。您…

day24-106.从中序与后序遍历序列构造二叉树

106.从中序与后序遍历序列构造二叉树 力扣题目链接(opens new window) 根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如&#xff0c;给出 中序遍历 inorder [9,3,15,20,7]后序遍历 postorder [9,15,7,20,3] 返回如下的二叉树&am…

前端跨域问题解决方法

跨域是WEB浏览器专有的同源限制访问策略。(后台接口调用和postman等工具会出现) 跨源资源共享&#xff08;CORS&#xff0c;或通俗地译为跨域资源共享&#xff09;是一种基于 HTTP 头的机制&#xff0c;该机制通过允许服务器标示除了它自己以外的其他源&#xff08;域、协议或端…

java项目打包运行报异常:Demo-1.0-SNAPSHOT.jar中没有主清单属性

检查后发现pom文件中有错误&#xff0c;需要添加build内容才能恢复正常。 添加下面文件后再次启动恢复正常。 <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactI…

Add-in Express for Microsoft Office and Delphi Crack

Add-in Express for Microsoft Office and Delphi Crack 适用于Microsoft Office和Delphi VCL的Add-in Express使您能够在几次点击中为Microsoft Office开发专业插件。它生成基于COM的项目&#xff0c;这些项目包含Microsoft Office外接程序或智能标记的所有必要功能&#xff0…