4_1_QFileDialog、QFile、对文件操作

1、效果

在这里插入图片描述
函数如下:
QFileDialog
在这里插入图片描述
QFile
在这里插入图片描述

2、代码

2.1 widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    Init();
}

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

void Widget::Init(){
     myfile = new QFile(this);

     errorMessageDialog = new QErrorMessage(this);
     errorLabel = new QLabel;
     int frameStyle = QFrame::Sunken | QFrame::Panel;
     errorLabel->setFrameStyle(frameStyle);

    connect(ui->btn_open,SIGNAL(clicked()),this,SLOT(doProcessButton_OpenFile()));
    connect(ui->btn_close,SIGNAL(clicked()),this,SLOT(doProcessButton_CloseFile()));
    connect(ui->btn_msg,SIGNAL(clicked()),this,SLOT(doProcessButton_errorMSG()));
}


void Widget::doProcessButton_errorMSG(){
    qDebug()<<"clicked msg button"<<endl;
    errorMessageDialog->showMessage(
            tr("This dialog shows and remembers error messages. "
               "If the checkbox is checked (as it is by default), "
               "the shown message will be shown again, "
               "but if the user unchecks the box the message "
               "will not appear again if QErrorMessage::showMessage() "
               "is called with the same message."));
    errorLabel->setText(tr("If the box is unchecked, the message "
                           "won't appear again."));

}

//打开按钮操作
void Widget::doProcessButton_OpenFile(){
    qDebug()<<"clicked open button"<<endl;
    //SOURCES(*.cpp);;HEADERS(*.h);;FORMS(*.ui) 第一个作为默认即.cpp为默认
    QString filename = QFileDialog::getOpenFileName(this,"get file","../QFile","SOURCES(*.cpp);;HEADERS(*.h);;FORMS(*.ui)");
    if(filename.isEmpty()){
        QMessageBox::warning(this,"warning","file can't select");
        return;
    }
    myfile->setFileName(filename);
    bool ret = myfile->open(QIODevice::ReadOnly|QIODevice::Text);
    if(!ret){
        QMessageBox::warning(this,"warning","open failed");
        return;
    }

    while(!myfile->atEnd()){//判断是否在末尾
        //方法一
//        QByteArray ba=myfile->read(MSG_LEN);//获取1024字节
//        QString str=QString(ba);//获取地址
//        ui->textEdit->append(str);//显示

        //方法二
        QByteArray ba=myfile->readLine();
        QString str=QString(ba);
        ui->textEdit->append(str);
//        QByteArray ba=myFile->readAll();
//        QString str=QString(ba);
//        ui->textEdit->append(str);
    }
    /*
    //文本形式读取(支持大文本文件读取)
    QTextStream text(myfile);
    while(!text.atEnd()){
        QString str=text.readAll();
        ui->textEdit->append(str);
    }

    */
    myfile->close();

}

//保存退出按钮
void Widget::doProcessButton_CloseFile(){
    qDebug()<<"clicked close button"<<endl;
    QString filename=QFileDialog::getSaveFileName(this,"save file","../QFile","Text(*.cpp *.h)");
    if(filename.isEmpty()){
        QMessageBox::warning(this,"warning","file can't select");
        return;
    }

    myfile->setFileName(filename);
    bool ret=myfile->open(QIODevice::WriteOnly|QIODevice::Text);
    if(!ret){
        QMessageBox::warning(this,"warning","open failed");
        return;
    }


    //QFile直接写
//    QString str=ui->textEdit->toPlainText();//Returns the text of the text edit as plain text.
//    myfile->write(str.toLocal8Bit());
//    myfile->close();

   //文本形式读取(支持大文本文件读取)
    QTextStream stream(myfile);
    QString str=ui->textEdit->toPlainText();
    stream<<str;
    stream.flush();//刷新数据
    myfile->close();


}



2.2 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QErrorMessage>
#include <QLabel>
#define MSG_LEN 1024

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;

    QFile *myfile;
    QLabel *errorLabel;
    QErrorMessage *errorMessageDialog;
    void Init();
private slots:
    void doProcessButton_OpenFile();
    void doProcessButton_CloseFile();
    void doProcessButton_errorMSG();
};

#endif // WIDGET_H



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值