qt5之QFile讀寫文件功能詳解
本文實(shí)例為大家分享了qt5之QFile讀寫文件功能的具體代碼,供大家參考,具體內(nèi)容如下
1、效果
讀寫文件用到的是QFile類,
這里,我顯示文件內(nèi)容用到的是 QTextEdit
2、創(chuàng)建打開和關(guān)閉按鈕
// 打開文件 ? ? btnOpenFile ? ? = new QToolButton; ? ? btnOpenFile->setText(tr("open a file")); ? ? btnOpenFile->setToolTip(tr("open a file")); ? ? connect(btnOpenFile, SIGNAL(clicked(bool)), this, SLOT(btnOpenFileSlot())); ? ? btnOpenFile->setIcon(QIcon(":/res/ico/dev/open")); ? ? btnOpenFile->setFixedSize(80, 48); ? ? btnOpenFile->setIconSize(QSize(80, 48)); ? ? ? // 關(guān)閉文件 ? ? btnCloseFile ? ?= new QToolButton; ? ? btnCloseFile->setText(tr("close file")); ? ? btnCloseFile->setToolTip(tr("close file")); ? ? connect(btnCloseFile, SIGNAL(clicked(bool)), this, SLOT(btnCloseFileSlot())); ? ? btnCloseFile->setIcon(QIcon(":/res/ico/dev/save")); ? ? btnCloseFile->setFixedSize(80, 48); ? ? btnCloseFile->setIconSize(QSize(80, 48));
3、打開文件
?/* ? ? ? ?getOpenFileName函數(shù)說明 ? ? ? ?函數(shù)原形: QStringList QFileDialog::getOpenFileNames( ? ? ? ?QWidget * parent = 0, ? ? ? ?const QString & caption = QString(), ? ?// ?打開文件對(duì)話框的標(biāo)題 ? ? ? ?const QString & dir = QString(), ? ? ? ? ? ?// ?查找目錄 ? ? ? ?const QString & filter = QString(), ? ? // ?設(shè)置需要過濾的文件格式 ? ? ? ?QString * selectedFilter = 0, ? ? ? ?Options options = 0) [static] ? ? ? ?*/ ? ? ? ?//---獲取文件名; ? ? QString qexeFullPath ? ?= QDir::currentPath(); ? ? QString fileName ? ? ? ?= QFileDialog :: getOpenFileName(this, tr("選擇一個(gè)文件"), qexeFullPath, "*.txt"); ? ? ? // 1、若沒有選擇文件 ? ? if (true ? ? ? ? ? ? ? ?== fileName.isEmpty()) ? ? { ? ? ? ? // 什么也不做 ? ? ? ? return; ? ? } ? ? ? // 2、選擇了文件,打開新選擇的文件前,檢查先前的文件 ? ? CheckFileClose(); ? ? ? // 3、 打開文件,顯示文件內(nèi)容 ? ? GetFileContext(fileName);
CheckFileClose函數(shù):
// 1、若已經(jīng)打開文件,且文件內(nèi)容發(fā)生變化,此時(shí)又打開文件,則提示是否保存先前的文件 ? ? bool fileIsOpen ? ? ? ? = fileReadWrite->isOpen(); ? ? // 1.1 若打開了, 沒有關(guān)閉 ? ? if (true ? ? ? ? ? ? ? ?== fileIsOpen) ? ? { ? ? ? ? // 1.1.1 若文件內(nèi)容發(fā)生變化 ? ? ? ? bool isChanged ? ? ?= GetTextEditContentIsChanged(); ? ? ? ? if (true ? ? ? ? ? ?== isChanged) ? ? ? ? { ? ? ? ? ? ? int ?okcancel ? = QMessageBox::information(this, tr("mention"), tr("dev tab, textEdit's content has changed, do U wanna save ?"), QMessageBox::Ok | QMessageBox::Cancel); ? ? ? ? ? ? ? // 點(diǎn)擊了是,則需要保存文件 ? ? ? ? ? ? if (QMessageBox::Ok == okcancel) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? SaveFile(); ? ? ? ? ? ? } ? ? ? ? ? ? ? // 點(diǎn)擊了否,什么也不做 ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? // 1.1.2 文件內(nèi)容沒有變化 ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? // 什么也不做 ? ? ? ? } ? ? ? ? ? // 1.1.3 關(guān)閉文件 ? ? ? ? fileReadWrite->close(); ? ? ? ? ? // 1.1.4 清空顯示內(nèi)容 ? ? ? ? textEditShowFile->clear(); ? ? ? ? ? // 1.1.5 設(shè)置為只讀 ? ? ? ? textEditShowFile->setReadOnly(true); ? ? ? ? ? // 1.1.6 清空文件內(nèi)容緩沖區(qū) ? ? ? ? textEditContent = QString(""); ? ? ? ? ? // 1.1.6 清除文件名 ? ? ? ? lineEditFileName->setText(""); ? ? } ? ? // 1.2文件沒有打開 ? ? else ? ? { ? ? ? ? // 文件沒有打開,什么也不做 ? ? ? }
GetFileContext函數(shù)代碼:
// 之前已經(jīng)保證文件關(guān)閉了,現(xiàn)在重新打開文件 ? ? ? // 2、 打開文件 ? ? fileReadWrite->setFileName(openNewFileName); ? ? bool openFlag ? ? ? ? ? = fileReadWrite->open(QIODevice ::ReadWrite | QIODevice ::Text); ? ? // 若打開失敗 ? ? if (false ? ? ? ? ? ? ? == openFlag) ? ? { ? ? ? ? QMessageBox::critical(this, tr("warning"), ?tr("open file err")); ? ? ? ? return; ? ? } ? ? ? // 保存文件名 ? ? openFileName ? ? ? ? ? ?= openNewFileName; ? ? ? // 3.1 刪除原有的內(nèi)容 ? ? textEditShowFile->clear(); ? ? ? // 3.2 顯示文件內(nèi)容 ? ? QTextStream textStream(fileReadWrite); ? ? while (!textStream.atEnd()) ? ? { ? ? ? ? //---QtextEdit按行顯示文件內(nèi)容 ? ? ? ? textEditShowFile->append(textStream.readLine()); ? ? } ? ? ? // 5、解除只讀 ? ? textEditShowFile->setReadOnly(false); ? ? ? // 6、臨時(shí)保存當(dāng)前打開文件內(nèi)容 ? ? textEditContent = textEditShowFile->toPlainText(); ? ? ? // 7、顯示打開的文件名 ? ? lineEditFileName->setText(openFileName);
4、關(guān)閉按鈕
下面做了關(guān)閉文件前的一些檢查
// 1、若已經(jīng)打開文件,且文件內(nèi)容發(fā)生變化,此時(shí)又打開文件,則提示是否保存先前的文件 ? ? bool fileIsOpen ? ? ? ? = fileReadWrite->isOpen(); ? ? // 1.1 若打開了, 沒有關(guān)閉 ? ? if (true ? ? ? ? ? ? ? ?== fileIsOpen) ? ? { ? ? ? ? // 1.1.1 若文件內(nèi)容發(fā)生變化 ? ? ? ? bool isChanged ? ? ?= GetTextEditContentIsChanged(); ? ? ? ? if (true ? ? ? ? ? ?== isChanged) ? ? ? ? { ? ? ? ? ? ? int ?okcancel ? = QMessageBox::information(this, tr("mention"), tr("dev tab, textEdit's content has changed, do U wanna save ?"), QMessageBox::Ok | QMessageBox::Cancel); ? ? ? ? ? ? ? // 點(diǎn)擊了是,則需要保存文件 ? ? ? ? ? ? if (QMessageBox::Ok == okcancel) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? SaveFile(); ? ? ? ? ? ? } ? ? ? ? ? ? ? // 點(diǎn)擊了否,什么也不做 ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? // 1.1.2 文件內(nèi)容沒有變化 ? ? ? ? else ? ? ? ? { ? ? ? ? ? ? // 什么也不做 ? ? ? ? } ? ? ? ? ? // 1.1.3 關(guān)閉文件 ? ? ? ? fileReadWrite->close(); ? ? ? ? ? // 1.1.4 清空顯示內(nèi)容 ? ? ? ? textEditShowFile->clear(); ? ? ? ? ? // 1.1.5 設(shè)置為只讀 ? ? ? ? textEditShowFile->setReadOnly(true); ? ? ? ? ? // 1.1.6 清空文件內(nèi)容緩沖區(qū) ? ? ? ? textEditContent = QString(""); ? ? ? ? ? // 1.1.6 清除文件名 ? ? ? ? lineEditFileName->setText(""); ? ? } ? ? // 1.2文件沒有打開 ? ? else ? ? { ? ? ? ? // 文件沒有打開,什么也不做 ? ? ? }
其中,SaveFile函數(shù)代碼如下:
?bool isOpen = fileReadWrite->isOpen(); ? ? // 若文件沒有打開 ? ? if (false ? == isOpen) ? ? { ? ? ? ? return; ? ? } ? ? ? // 關(guān)閉文件 ? ? fileReadWrite->close(); ? ? ? fileReadWrite->open(QIODevice ::WriteOnly | QIODevice ::Text | QIODevice::Truncate); ? ? QString writeStr ? ?= textEditShowFile->toPlainText(); #ifdef QT_DEBUG ? ? qDebug() << "文件內(nèi)容 = " << writeStr; #endif ? ? ? // 文件打開了,現(xiàn)在關(guān)閉 ? ? QTextStream outFile(fileReadWrite); ? ? outFile << writeStr << endl; ? ? outFile.flush();
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
實(shí)例講解C語言編程中的結(jié)構(gòu)體對(duì)齊
這篇文章主要介紹了C語言編程中的結(jié)構(gòu)體對(duì)齊,值得注意的是一些結(jié)構(gòu)體對(duì)齊的例子在不同編譯器下結(jié)果可能會(huì)不同,需要的朋友可以參考下2016-04-04C++求所有頂點(diǎn)之間的最短路徑(用Dijkstra算法)
這篇文章主要為大家詳細(xì)介紹了C++用Dijkstra算法求所有頂點(diǎn)之間的最短路徑,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C++中用棧來判斷括號(hào)字符串匹配問題的實(shí)現(xiàn)方法
這篇文章主要介紹了C++中用棧來判斷括號(hào)字符串匹配問題的實(shí)現(xiàn)方法,是一個(gè)比較實(shí)用的算法技巧,包含了關(guān)于棧的基本操作,需要的朋友可以參考下2014-08-08