Qt打开读取和保存写入文件
2016-11-29 19:25
543 查看
打开
在头文件mainwindow.h中添加打开文件需要的包
#include<QMessageBox>
#include<QDebug>
#include<QMainWindow>
#include<QFile>
#include<QFileDialog>
#include<QDir>
#include<QTextStream>
并且添加私有信号槽
private slots:
void openFileSlot();
在mainwindow.cpp源文件中添加打开文件函数
void MainWindow::openFileSlot()
{
//获取文件名称
QString fileName = QFileDialog::getOpenFileName(this,"OpenFile",QDir::currentPath());
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error","Please select a txt");
return;
}
else
{
QFile *file = new QFile;
file->setFileName(fileName);//新建一个文件对象,并且把它设置为上面获取的文件
bool ok=file->open(QIODevice::ReadOnly);//设置打开模式
if(ok)//如果可以打开
{
//将文件与文本流相关联
QTextStream in(file);
ui->textEdit->setText(in.readAll());//读取该文件的所有内容
file->close();//关闭文件
delete file();//释放文件进程
}
else
{
QMessageBox::information(this,"Error Box","FileOpen Error"+file->errorString());
}
}
在MainWindow构造函数中添加信号与槽的连接信息
QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
保存
保存就是打开文件的逆过程
同样,在mainwindow.h添加私有信号槽
private slots:
void saveFileSlot();
在mainwindow.cpp中添加保存的函数
void MainWindow::saveFileSlot()
{
QString filename = QFileDialog::getSaveFileName(this,"Save File","D:\\");//获取需要保存成的文件名
if(filename.isEmpty())
{
QMessageBox::information(this,"ErrorBox","Please input the filename");
return;
}
else
{
QFile *file = new QFile;
file->setFileName(filename);
bool ok = file->open(QIODevice::WriteOnly)
if(ok)
{
QTextStream out(file);
out<<ui->textEdit->toPlainText(); //转化成纯文本
file->close();
delete(file);
}
else
{
QMessageBox::information(this,"ErrorBox","file fail to save")
}
}
}
在头文件mainwindow.h中添加打开文件需要的包
#include<QMessageBox>
#include<QDebug>
#include<QMainWindow>
#include<QFile>
#include<QFileDialog>
#include<QDir>
#include<QTextStream>
并且添加私有信号槽
private slots:
void openFileSlot();
在mainwindow.cpp源文件中添加打开文件函数
void MainWindow::openFileSlot()
{
//获取文件名称
QString fileName = QFileDialog::getOpenFileName(this,"OpenFile",QDir::currentPath());
if(fileName.isEmpty())
{
QMessageBox::information(this,"Error","Please select a txt");
return;
}
else
{
QFile *file = new QFile;
file->setFileName(fileName);//新建一个文件对象,并且把它设置为上面获取的文件
bool ok=file->open(QIODevice::ReadOnly);//设置打开模式
if(ok)//如果可以打开
{
//将文件与文本流相关联
QTextStream in(file);
ui->textEdit->setText(in.readAll());//读取该文件的所有内容
file->close();//关闭文件
delete file();//释放文件进程
}
else
{
QMessageBox::information(this,"Error Box","FileOpen Error"+file->errorString());
}
}
在MainWindow构造函数中添加信号与槽的连接信息
QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot()));
保存
保存就是打开文件的逆过程
同样,在mainwindow.h添加私有信号槽
private slots:
void saveFileSlot();
在mainwindow.cpp中添加保存的函数
void MainWindow::saveFileSlot()
{
QString filename = QFileDialog::getSaveFileName(this,"Save File","D:\\");//获取需要保存成的文件名
if(filename.isEmpty())
{
QMessageBox::information(this,"ErrorBox","Please input the filename");
return;
}
else
{
QFile *file = new QFile;
file->setFileName(filename);
bool ok = file->open(QIODevice::WriteOnly)
if(ok)
{
QTextStream out(file);
out<<ui->textEdit->toPlainText(); //转化成纯文本
file->close();
delete(file);
}
else
{
QMessageBox::information(this,"ErrorBox","file fail to save")
}
}
}
相关文章推荐
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- 如何从内核模式设备驱动程序中打开文件以及如何读取或写入文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- QT打开和保存文件对话框
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 将 Excel 2007 读取到 Byte[], 然后再保存到新的Excel文件中, 这时打开新文件会出错.
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- 在linux下用qt打开或直接读取文件及其中的问题!(system(),qfile,qprocess)
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- 一段代码让你学会Qt的中文显示,以及文件打开,保存操作
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- fopen(),fwrite() ,fgets() 打开,写入和读取文件
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- 如何从内核模式设备驱动程序中打开一个文件以及如何读取或写入文件(来自微软官网)
- c#中创建、打开、读取、写入、保存Excel的一般性代码
- 如何从内核模式设备驱动程序中打开一个文件以及如何读取或写入文件
- C# Excel 创建、打开、读取、写入、保存的一般性代码
- 插入数据读取数据库写入到Text文件保存
- C#创建、打开、读取、写入、保存Excel,Excel打印输出