qfile 创建文件_Qt之二进制文件读写
生活随笔
收集整理的這篇文章主要介紹了
qfile 创建文件_Qt之二进制文件读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
點擊上方“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”,在將數據寫到文件時,完全使用數據的二進制原始內容,每個字節都有具體的定義,在讀出數據時,只需要根據每個字節的定義讀出數據即可
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的qfile 创建文件_Qt之二进制文件读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 真心话的问题题目大全109个
- 下一篇: 关于羊的网名86个