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