抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

misakivv的博客

霜蹄千里骏,风翮九霄鹏

QT文件操作

QFile读写文本

提供读取写入文件的接口

文件名通常在构造函数中传递

1
2
3
4
5
6
QIDDevice::Readonly:以只读的方式打开文件,用于载入文件
QIDDevice::Writeonly:以只写方式打开文件,用于保存文件
QIDDevice::ReadWrite:以读写方式打开
QIDDevice::Append:以添加模式打开,新写入文件的数据添加到文件末尾
QIDDevice::Truncate:以裁取的方式打开文件,文件原有内容会被删除
QIDDevice::Text:以文本打开文件

流程:文件–>打开方式–>关闭

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
//main

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QTextEdit>
#include<QFile>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QPushButton>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
Ui::MainWindow *ui;
private:
//用于读取文件后显示
QTextEdit *testEdit;
QFile file; //QFile 类型对象
QHBoxLayout *hBoxLayout; //水平布局
QVBoxLayout *vBoxLayout; //垂直布局

QWidget *hwidget; //水平布局 widget
QWidget *vwideget; //垂直布局 widget

QPushButton *openPushButton; //打开文件按钮
QPushButton *closePushButton; //关闭文件按钮

private slots:
bool openFile(); //打开文本文件
void closeFile(); //关闭文本文件
};
#endif // MAINWINDOW_H
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//mainwindow.h

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0,800,480);
textEdit = new QTextEdit();
vBoxLayout = new QVBoxLayout();
hBoxLayout = new QHBoxLayout();
vwideget = new QWidget ();
hwidget = new QWidget ();
openPushButton = new QPushButton ();
closePushButton = new QPushButton();
openPushButton->setMinimumHeight(50);
openPushButton->setMaximumWidth(120);
closePushButton->setMinimumHeight(50);
closePushButton->setMaximumWidth(120);
openPushButton->setText("open");
closePushButton->setText("close");

closePushButton->setEnabled(false);

hBoxLayout->addWidget(openPushButton);
hBoxLayout->addWidget(closePushButton);
hwidget->setLayout(hBoxLayout);

vBoxLayout->addWidget(textEdit);
vBoxLayout->addWidget(hwidget);
vwideget->setLayout(vBoxLayout);

setCentralWidget(vwideget);

connect(openPushButton,SIGNAL(clicked()),
this,SLOT(openFile()));
connect(closePushButton,SIGNAL(clicked()),
this,SLOT(openFile()));


}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
file.setFileName(fileName);
if(!file.exists())
return false;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
textEdit->setPlainText(file.readAll());
openPushButton->setEnabled(false);
closePushButton->setEnabled(true);

file.close();
return true;
}
void MainWindow::closeFile()
{
if(!openPushButton->isEnabled()){

QString str = textEdit->toPlainText();
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;

QByteArray strBytes = str.toUtf8();
file.write(strBytes,strBytes.length());
textEdit->clear();
openPushButton->setEnabled(true);
closePushButton->setEnabled(false);
}
}

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog> //标准通用对话框
#include <QDebug> //信息输出头文件
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0,800,480); //设置窗口的位置与大小
//布局设置
textEdit = new QTextEdit();
vBoxLayout = new QVBoxLayout();
hBoxLayout = new QHBoxLayout();
vwideget = new QWidget ();
hwidget = new QWidget ();
openPushButton = new QPushButton ();
closePushButton = new QPushButton();
openPushButton->setMinimumHeight(50);
openPushButton->setMaximumWidth(120);
closePushButton->setMinimumHeight(50);
closePushButton->setMaximumWidth(120);
//设置两个按钮的文本
openPushButton->setText("打开");
closePushButton->setText("关闭");
//设置关闭按钮为不可用属性,需要打开文件才设置为可用属性
closePushButton->setEnabled(false);
//水平布局
hBoxLayout->addWidget(openPushButton);
hBoxLayout->addWidget(closePushButton);
hwidget->setLayout(hBoxLayout);
//垂直布局
vBoxLayout->addWidget(textEdit);
vBoxLayout->addWidget(hwidget);
vwideget->setLayout(vBoxLayout);
//居中
setCentralWidget(vwideget);
//信号槽连接

connect(openPushButton,SIGNAL(clicked()),
this,SLOT(openFile()));
connect(closePushButton,SIGNAL(clicked()),
this,SLOT(openFile()));


}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this);//获取文件的路径
file.setFileName(fileName); //指向文件
if(!file.exists()) //判断文件是否存在
return false;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) //以读写的方式打开
return false;
textEdit->setPlainText(file.readAll()); //读取文本到textEdit
openPushButton->setEnabled(false); //设置打开按钮不可用,需要关闭再打开
closePushButton->setEnabled(true); //设置关闭按钮为可用属性


file.close(); //关闭文件
return true;
}
void MainWindow::closeFile()
{
if(!openPushButton->isEnabled()){ //检测打开按钮是否可用,不可用时,说明已经打开了文件
QString str = textEdit->toPlainText(); //获取textEdit的文本内容
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) //以只读的方式打开
return;

QByteArray strBytes = str.toUtf8(); //转换为字节数组
file.write(strBytes,strBytes.length()); //写入文件

textEdit->clear(); //清空textEdit的显示内容
openPushButton->setEnabled(true); //重新设置打开何关闭按钮的属性
closePushButton->setEnabled(false);
}
}

2.QTextStream读写文本

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0,800,480);
textEdit = new QTextEdit();
vBoxLayout = new QVBoxLayout();
hBoxLayout = new QHBoxLayout();
vwideget = new QWidget ();
hwidget = new QWidget ();
openPushButton = new QPushButton ();
closePushButton = new QPushButton();
openPushButton->setMinimumHeight(50);
openPushButton->setMaximumWidth(120);
closePushButton->setMinimumHeight(50);
closePushButton->setMaximumWidth(120);
openPushButton->setText("open");
closePushButton->setText("close");

closePushButton->setEnabled(false);

hBoxLayout->addWidget(openPushButton);
hBoxLayout->addWidget(closePushButton);
hwidget->setLayout(hBoxLayout);

vBoxLayout->addWidget(textEdit);
vBoxLayout->addWidget(hwidget);
vwideget->setLayout(vBoxLayout);

setCentralWidget(vwideget);

connect(openPushButton,SIGNAL(clicked()),
this,SLOT(openFile()));
connect(closePushButton,SIGNAL(clicked()),
this,SLOT(openFile()));


}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
file.setFileName(fileName);
if(!file.exists())
return false;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream stream(&file);
textEdit->setPlainText(file.readAll());
openPushButton->setEnabled(false);
closePushButton->setEnabled(true);

file.close();
return true;
}
void MainWindow::closeFile()
{
if(!openPushButton->isEnabled()){

QString str = textEdit->toPlainText();
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream stream(&file);
QString str_1 = textEdit->toPlainText();
stream <<str_1;
//QByteArray strBytes = str.toUtf8();
//file.write(strBytes,strBytes.length());
textEdit->clear();
openPushButton->setEnabled(true);
closePushButton->setEnabled(false);
}
}

QT多线程

QThread类提供一种独立于平台的方法管理线程。

继承QThread的线程

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
52
53
54
55
56
57
58
//mainwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include<QMainWindow>
#include<QThread>
#include<QDebug>
#include<QPushButton>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class WorkerThread;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private:
Ui::MainWindow *ui;
private:
WorkerThread *workThread; //声明对象
QPushButton *pushButton; //声明一个按钮,使用此按钮点击后开启线程

private slots: //槽函数,用于接受线程点击后开启线程
void headleResults(const QString &result);
void pushButtonClicked(); //点击按钮开启线程
};
//新建一个WorkThread类继承于QThread
class WorkerThread : public QThread
{
Q_OBJECT //用到信号槽即需要此宏定义

public:
WorkerThread(QWidget *parent=nullptr){
Q_UNUSED(parent);

}
//重写run()方法
void run() override{
QString result="线程开启成功";

sleep(2); //延迟2s
emit resultReady(result); //发送结果准备好的信号
}
signals:
//声明一个信号
void resultReady(const QString &s);
};
#endif // MAINWINDOW_H

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
//mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setGeometry(0,0,800,480); //设置位置与大小
pushButton=new QPushButton(this); //对象实例化
workerThread = new WorkerThread(this); //线程对象实例化
//按钮设置大小与文本
pushButton->resize(100,40);
pushButton->setText("圆神,启动");
//信号槽连接
connect(workerThread,SIGNAL(resultReady(QString)),this,SLOT(handleResults(QString)));
connect(pushButton,SIGNAL(clicked()),this,SLOT(pushButtonClicked()));
}

MainWindow::~MainWindow()
{
workerThread->quit(); //进程退出
if(workerThread->wait(2000)){ //阻塞等待2000ms检查一次进程是否已经退出

qDebug()<<"Thread over!"<<endl;
}
delete ui;
}
void MainWindow::handleResults(const QString &result){
qDebug()<<result<<endl; //打印线程发送过来的结果
}
void MainWindow::pushButtonClicked()
{
if(!workerThread->isRunning()) //检查线程是否再运行,如果没有则开始运行
workerThread->start();
}

image-20231107154643093.png image-20231107154609600.png

评论