QT-功能-实现文件操作一系列功能(打开,写入,更改,删除)


此系列展示均为创建有ui界面的工程,但不对ui界面进行操作,且实例基类均mainwindow

1.打开文件

代码实现

mainwindow.h中添加一个行编辑框和按钮控件作为简单界面布局,并添加简单的显示关联槽函数。

#include <QLineEdit>
#include <QPushButton>

private:
    QLineEdit *filename;
    QPushButton *button;
private slots:
    void showFiles();

maindwindow.cpp中对其实现
添加头文件

#include <QFileDialog>
	//实例单行文本控件
    filename = new QLineEdit(this);
    //位置
    filename->setGeometry(QRect(50,50,230,25));
    //实例按钮
    button = new QPushButton(this);
    //按钮位置
    button->setGeometry(QRect(280,50,80,25));
    //按钮名
    button->setText("浏览");
    //按钮点击事件
    connect(button,SIGNAL(clicked()),this,SLOT(showFiles()));

槽函数实现

//按钮点击方法
void MainWindow::showFiles()
{
        //定义变量str 接收QFileDialog 对话框获取的文件路径
        QString str = QFileDialog::getOpenFileName(this,"文件游览","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)");
        //将变量绑定QLineEdit 控件
        filename->setText(str.toUtf8());
}

效果展示

在这里插入图片描述
在这里插入图片描述
需要注意的是,此工程文件创建在E盘中,所以文件定位到E盘的根目录下,这里作为例子不对其做深究,

2.写入文件

代码实现

mainwindow.h中同样添加行编辑框和按钮控件作为界面主要内容

#include <QPushButton>
#include <QLineEdit>
private:
    QPushButton *button;
    QLineEdit *edit;
    QLineEdit *content;
private slots:
    void createFile();

mainwindow.cpp
添加头文件

#include <QFile>
#include <QFileDialog>
	//文件操作————写入文件内容
    //实例
    edit = new QLineEdit(this);
    //位置
    edit->setGeometry(QRect(50,50,200,25));
    //值,可改变相应的位置
    edit->setText("D:/Qt02.txt");

    //实例
    content = new QLineEdit(this);
    //位置
    content->setGeometry(QRect(50,100,200,25));
    //值
    content->setText("写入文件的内容");
    //实例button
    button = new QPushButton(this);
    //位置
    button->setGeometry(QRect(270,50,80,25));
    //值
    button->setText("创建");
    //事件
    connect(button,SIGNAL(clicked()),this,SLOT(createFile()));

槽函数实现

void MainWindow::createFile()
{
    //实例QFile
    QFile file(edit->text());
    //判断文件是否存在
    if(file.exists())
    {
        QMessageBox::warning(this,"创建文件","文件已经存在!");
    }else
    {
        //存在打开,不存在创建
        file. open (QIODevice::ReadWrite | QIODevice::Text);
        //写入内容,这里需要转码,否则报错。
        QByteArray str = content->text().toUtf8();
        //写入QByteArray 格式字符串
        file.write(str);
        //提示成功
        QMessageBox::warning(this,"创建文件","文件创建成功!");
    }
    //关闭文件
    file.close ();
}

效果展示

在这里插入图片描述
在D盘中故可以找到新建的文件
在这里插入图片描述
在这里插入图片描述

3.文件修改

代码实现

mainwindow.h

添加头文件

#include <QPushButton>
#include <QTextEdit>
private:
    QPushButton *browseBt;
    QPushButton *saveBt;
    QTextEdit *edit;
    QTextEdit *content;
private slots:
	void browseFile();		//游览文件	
    void saveFile();		//保存文件

mainwindow.cpp
添加头文件

#include <QFile>
#include <QFileDialog>
	//实例文件路径
    edit = new QTextEdit(this);
    edit->setGeometry(QRect(50,50,240,26));
    //浏览文件按钮
    browseBt = new QPushButton(this);
    browseBt->setGeometry(QRect(290,50,80,25));
    browseBt->setText("浏览文件");
    connect(browseBt,SIGNAL(clicked()),this,SLOT(browseFile()));
    //显示文件内容
    content = new QTextEdit(this);
    content->setGeometry(QRect(50,80,320,150));
    //修改完毕后,保存文件
    saveBt = new QPushButton(this);
    saveBt->setGeometry(QRect(290,240,80,25));
    saveBt->setText("保存");
    connect(saveBt,SIGNAL(clicked()),this,SLOT(saveFile()));

槽函数实现

//浏览文件
void MainWindow::browseFile()
{
    //QTextCodec * codec =  QTextCodec::codecForName("GBk");
    //定义变量str 接收QFileDialog 对话框获取的文件路径
    QString str = QFileDialog::getOpenFileName(this,"open file","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)");
    //将变量绑定QTextEdit 控件
    edit->setText(str.toUtf8());
    //判断是否选择文件
    if(edit->toPlainText().isEmpty())
    {
        return;
    }
    QFile file(edit->toPlainText());
    //判断文件是否打开成功
    if(!file. open (QIODevice::ReadOnly|QIODevice::Text))
    {
        QMessageBox::warning(this,"打开文件","打开文件失败!");
        return;
    }
    QTextStream ts(&file);
    //文件转码为utf-8的形式
    ts.setCodec("UTF-8");
    //循环文档数据至结尾
    while(!ts.atEnd())
    {
        //将全部数据绑定至content 控件
        content->setPlainText(ts.readAll());
    }
    //关闭文档
    file. close ();
}

//保存文件
void MainWindow::saveFile()
{
    QFile file(edit->toPlainText());
    file. open (QIODevice::ReadWrite | QIODevice::Text);
    //写入内容,这里需要转码,否则报错。
    QByteArray str = content->toPlainText().toUtf8();
    //写入QByteArray 格式字符串
    file.write(str);
    //提示成功
    QMessageBox::warning(this,"修改文件","文件修改成功!");
    file. close ();
}

效果展示

在这里插入图片描述
以下面的两个txt文档为例打开
在这里插入图片描述
在这里插入图片描述
需要注意的是,在这个程序中,由于所有qt支持编码为UTF-8。所以编写出的界面程序依旧是UTF-8形式的。而windows中所创建的txt文档是GBk编码的,所以在打开文件时候需要将GBK编码转换成程序支持的UTF-8编码。这样才不会出现乱码

	//文件转码为utf-8的形式
    ts.setCodec("UTF-8");

4.删除文件

代码实现

mainwindow.h

添加头文件

#include <QLineEdit>
#include <QPushButton>
private:
    QPushButton *browseBt;
    QPushButton *deleteBt;
    QLineEdit *filePath;
private slots:
    void browseFile();
    void deleteFile();

mainqwindow.cpp

添加头文件

#include <QFileDialog>
	filePath = new QLineEdit(this);
    filePath->setGeometry(QRect(50,50,200,25));

    //游览文件按钮
    browseBt = new QPushButton(this);
    browseBt->setGeometry(QRect(270,50,80,25));
    browseBt->setText("游览文件");
    connect(browseBt,SIGNAL(clicked()),this,SLOT(browseFile()));

    //删除文件按钮
    deleteBt = new QPushButton(this);
    deleteBt->setGeometry(QRect(50,100,80,25));
    deleteBt->setText("删除文件");
    connect(deleteBt,SIGNAL(clicked()),this,SLOT(deleteFile()));
**槽函数**
void MainWindow::browseFile()
{
    //定义变量str 接收QFileDialog 对话框获取的文件路径
    QString str = QFileDialog::getOpenFileName(this,"open file","/home",
                                             "text file(*.txt);;C file(*.cpp);;All file(*.*)");
    //将变量绑定QLineEdit控件
    filePath->setText(str.toUtf8());
}

void MainWindow::deleteFile()
{
    //删除参数中的内容
    QFile::remove(filePath->text());
}

效果展示

在这里插入图片描述
这里删除刚才创建的text01.txt文件
在这里插入图片描述

发布了43 篇原创文章 · 获赞 7 · 访问量 9042

猜你喜欢

转载自blog.csdn.net/qq_41488943/article/details/96594571