日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

qt5之QFile讀寫文件功能詳解_C 語言

作者:PandaMohist ? 更新時間: 2022-04-12 編程語言

本文實(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

欄目分類
最近更新