参考博客:https://blog.csdn.net/WQ_1996/article/details/78237151
环境:Qt5.8 win7
一、创建一个名为“file_operate”的项目文件
生成file_operate.pro、mainwindow.h、mainwindow.cpp、main.cpp、mainwindow.ui五个文件。
二、打开mainwindow.ui
1.添加“文件”菜单,并添加“new_file”、“open_file”、“save_file”、“save_as_file”四个Action。
2.向窗口中拖入“Text Edit”控件。并在属性编辑的“geometry“X、Y、宽度、高度分别设置为150,100,500,600。
3.添加槽函数
在ui设计界面下方的“Action Editor”中,选中动作“new_file",右键选择“转到槽...”,选择"triggered()",进入槽函数编辑模式。
以此类推,添加open_file,save_file,save_as_file槽函数。
三、.进入编辑模式(代码转载自https://blog.csdn.net/WQ_1996/article/details/78237151)
1. mainwindow.h代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_new_file_triggered();
void on_open_file_triggered();
void on_save_as_file_triggered();
void on_save_file_triggered();
void on_closeEvent(QCloseEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
2.mainwindow.cpp代码如下:
/*
class QFileDialog
//QFileDialog类继承自QDialog类。QFileDialog类为用户提供了选择文件/路径的对话框。
class QFileDevice
//QFileDevice类继承自QFileDevice类,派生类有QFile、QSaveFile。QFileDevice类提供了用于读写打开文件的接口。
class QFile
//QFile类继承自QFileDevice类,派生类有QTemporaryFile。QFile类提供了用于读写文件的接口。
class QTextStream
//QTextStream类提供了方便的读写文本的界面。
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
int flag_isOpen = 0;
int flag_isNew = 0;
QString Last_FileName;
QString Last_FileContent;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_open_file_triggered()
{
QString fileName;
fileName = QFileDialog::getOpenFileName(this,"Open File","","Text File(*.txt)");
if(fileName == "")
{
return ;
}
else
{
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","open file error!");
return;
}
else
{
if(!file.isReadable())
QMessageBox::warning(this,"error","this file is not readable!");
else
{
QTextStream textStream(&file);
while(!textStream.atEnd())
{
ui->textEdit->setPlainText(textStream.readAll());
}
ui->textEdit->show();
file.close();
flag_isOpen = 1;
Last_FileName = fileName;
}
}
}
}
void MainWindow::on_new_file_triggered()
{
ui->textEdit->clear();
ui->textEdit->setHidden(false);
flag_isNew = 1;
flag_isOpen = 1;
}
void MainWindow::on_save_as_file_triggered()
{
QFileDialog fileDialog;
QString fileName = fileDialog.getSaveFileName(this,"Open File","","Text File(*.txt)");
if(fileName == "")
{
return;
}
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","open file failure!");
return;
}
else
{
QTextStream textStream(&file);
QString str = ui->textEdit->toPlainText();
textStream<textEdit->toPlainText()=="")
{
QMessageBox::warning(this,"error","content can not be none!",QMessageBox::Ok);
}
else
{
QFileDialog fileDialog;
QString str = fileDialog.getSaveFileName(this,"Open File","","Text File(*.txt)");
if(str == "")
{
return;
}
QFile filename(str);
if(!filename.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","Open File Error!");
return;
}
else
{
QTextStream textStream(&filename);
QString str = ui->textEdit->toPlainText();
textStream << str;
Last_FileContent =str;
}
QMessageBox::information(this,"Ssve File","Save File Success",QMessageBox::Ok);
filename.close();
flag_isNew = 0;
Last_FileName = str;
}
}
else
{
if(flag_isOpen)
{
QFile file(Last_FileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this,"error","Open File Faile");
return;
}
else
{
QTextStream textString(&file);
QString str = ui->textEdit->toPlainText();
textString << str;
Last_FileContent = str;
file.close();
}
}
else
{
QMessageBox::warning(this,"Warning","Please new or open a file");
return;
}
}
}
void MainWindow::on_closeEvent(QCloseEvent *event)
{
if(ui->textEdit->toPlainText() == Last_FileContent)
event->accept();
else
{
if(QMessageBox::warning(this,"Warning","the file have not save,ensure exit?",QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
{
event->accept();
}
else
event->ignore();
}
}
3.mian.cpp内容不做更改
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
四、实验结果如下: