本文实例为大家分享了qt5之QFile读写文件功能的具体代码,供大家参考,具体内容如下
1、效果
读写文件用到的是QFile类,
这里,我显示文件内容用到的是 QTextEdit
2、创建打开和关闭按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 打开文件 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)); // 关闭文件 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、打开文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* getOpenFileName函数说明 函数原形: QStringList QFileDialog::getOpenFileNames( QWidget * parent = 0, const QString & caption = QString(), // 打开文件对话框的标题 const QString & dir = QString(), // 查找目录 const QString & filter = QString(), // 设置需要过滤的文件格式 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、 打开文件,显示文件内容 GetFileContext(fileName); |
CheckFileClose函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// 1、若已经打开文件,且文件内容发生变化,此时又打开文件,则提示是否保存先前的文件 bool fileIsOpen = fileReadWrite->isOpen(); // 1.1 若打开了, 没有关闭 if ( true == fileIsOpen) { // 1.1.1 若文件内容发生变化 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); // 点击了是,则需要保存文件 if (QMessageBox::Ok == okcancel) { SaveFile(); } // 点击了否,什么也不做 else { } } // 1.1.2 文件内容没有变化 else { // 什么也不做 } // 1.1.3 关闭文件 fileReadWrite->close(); // 1.1.4 清空显示内容 textEditShowFile->clear(); // 1.1.5 设置为只读 textEditShowFile->setReadOnly( true ); // 1.1.6 清空文件内容缓冲区 textEditContent = QString( "" ); // 1.1.6 清除文件名 lineEditFileName->setText( "" ); } // 1.2文件没有打开 else { // 文件没有打开,什么也不做 } |
GetFileContext函数代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
// 之前已经保证文件关闭了,现在重新打开文件 // 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 删除原有的内容 textEditShowFile->clear(); // 3.2 显示文件内容 QTextStream textStream(fileReadWrite); while (!textStream.atEnd()) { //---QtextEdit按行显示文件内容 textEditShowFile->append(textStream.readLine()); } // 5、解除只读 textEditShowFile->setReadOnly( false ); // 6、临时保存当前打开文件内容 textEditContent = textEditShowFile->toPlainText(); // 7、显示打开的文件名 lineEditFileName->setText(openFileName); |
4、关闭按钮
下面做了关闭文件前的一些检查
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// 1、若已经打开文件,且文件内容发生变化,此时又打开文件,则提示是否保存先前的文件 bool fileIsOpen = fileReadWrite->isOpen(); // 1.1 若打开了, 没有关闭 if ( true == fileIsOpen) { // 1.1.1 若文件内容发生变化 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); // 点击了是,则需要保存文件 if (QMessageBox::Ok == okcancel) { SaveFile(); } // 点击了否,什么也不做 else { } } // 1.1.2 文件内容没有变化 else { // 什么也不做 } // 1.1.3 关闭文件 fileReadWrite->close(); // 1.1.4 清空显示内容 textEditShowFile->clear(); // 1.1.5 设置为只读 textEditShowFile->setReadOnly( true ); // 1.1.6 清空文件内容缓冲区 textEditContent = QString( "" ); // 1.1.6 清除文件名 lineEditFileName->setText( "" ); } // 1.2文件没有打开 else { // 文件没有打开,什么也不做 } |
其中,SaveFile函数代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
bool isOpen = fileReadWrite->isOpen(); // 若文件没有打开 if ( false == isOpen) { return ; } // 关闭文件 fileReadWrite->close(); fileReadWrite->open(QIODevice ::WriteOnly | QIODevice ::Text | QIODevice::Truncate); QString writeStr = textEditShowFile->toPlainText(); #ifdef QT_DEBUG qDebug() << "文件内容 = " << writeStr; #endif // 文件打开了,现在关闭 QTextStream outFile(fileReadWrite); outFile << writeStr << endl; outFile.flush(); |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/HK_5788/article/details/80959585