網(wǎng)站首頁 編程語言 正文
本文實(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(), ? ?// ?打開文件對話框的標(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("選擇一個文件"), qexeFullPath, "*.txt"); ? ? ? // 1、若沒有選擇文件 ? ? if (true ? ? ? ? ? ? ? ?== fileName.isEmpty()) ? ? { ? ? ? ? // 什么也不做 ? ? ? ? return; ? ? } ? ? ? // 2、選擇了文件,打開新選擇的文件前,檢查先前的文件 ? ? CheckFileClose(); ? ? ? // 3、 打開文件,顯示文件內(nèi)容 ? ? GetFileContext(fileName);
CheckFileClose函數(shù):
// 1、若已經(jīng)打開文件,且文件內(nèi)容發(fā)生變化,此時又打開文件,則提示是否保存先前的文件 ? ? 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、臨時保存當(dāng)前打開文件內(nèi)容 ? ? textEditContent = textEditShowFile->toPlainText(); ? ? ? // 7、顯示打開的文件名 ? ? lineEditFileName->setText(openFileName);
4、關(guān)閉按鈕
下面做了關(guān)閉文件前的一些檢查
// 1、若已經(jīng)打開文件,且文件內(nèi)容發(fā)生變化,此時又打開文件,則提示是否保存先前的文件 ? ? 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();
原文鏈接:https://blog.csdn.net/HK_5788/article/details/80959585
相關(guān)推薦
- 2022-05-25 swagger 3.0.0 版本和springboot整合啟動失敗
- 2023-11-18 python字符串string的截取;獲取字符串內(nèi)的一串
- 2022-05-14 聊聊python?邏輯運(yùn)算及奇怪的返回值(not,and,or)問題_python
- 2022-12-28 Linux下rm誤刪除文件的三種恢復(fù)方法_linux shell
- 2023-01-23 重啟后nvidia-smi命令不可執(zhí)行出現(xiàn)“Make?sure?that?the?latest?NV
- 2021-12-18 C/C++?Qt?數(shù)據(jù)庫與TreeView組件綁定詳解_C 語言
- 2022-09-04 C++實(shí)現(xiàn)ETW進(jìn)行進(jìn)程變動監(jiān)控詳解_C 語言
- 2022-10-29 利用platform編寫驅(qū)動控制樹莓派4B io口
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支