QT写入文本文件

#include "intercalate.h"
#include <QMessageBox>
#include <QTextStream>
#include <QDebug>
#include <QtCore/QTextCodec>
InterCalate::InterCalate(QWidget *parent) : QWidget (parent){
setupUi(this);
QAction *select = new QAction(tr("确定"),this);
select->setSoftKeyRole(QAction::PositiveSoftKey);
this->addAction(select);
QAction *quet = new QAction(tr("取消"),this);
quet->setSoftKeyRole(QAction::NegativeSoftKey);
this->addAction(quet);
connect(select,SIGNAL(triggered(bool)),this,SLOT(writefile(bool)));
connect(quet,SIGNAL(triggered(bool)),this,SLOT(cancelpushBtn(bool)));
readfile();
}
InterCalate::~InterCalate(){


}
void InterCalate::readfile(){
#ifdef _DEBUG
QFile file1("c:\\sss.txt");
#else 
QFile file1("e:\\project\\sss.txt");
#endif
if(!file1.open(QFile::ReadWrite|QFile::Text)){
QMessageBox::warning(this,tr("open"),tr("open failed!"));
}
QTextCodec *code=QTextCodec::codecForName("GB2312");//
QTextStream stream(&file1);
stream.setCodec(code);
QString line = stream.readLine();
if(line == ",,"||line == ""){
nameLineEdit->setText("");
telLineEdit->setText("");
untieLineEdit->setText("");
}
else{
QList<QString> elems = line.split(",");
nameLineEdit->setText(elems.at(0));
telLineEdit->setText(elems.at(1));
untieLineEdit->setText(elems.at(2));
}
file1.close();
}
void InterCalate::writefile(bool chick){
if(!chick){
#ifdef _DEBUG
QFile file("c:\\sss.txt");
#else 
QFile file("e:\\project\\sss.txt");
#endif
if(!file.open(QFile::WriteOnly|QFile::Text|QFile::Truncate)){
QMessageBox::warning(this,tr("open"),tr("open failed!"));
}

QString str;
str.append(nameLineEdit->text());
str.append(",");
str.append(telLineEdit->text());
str.append(",");
str.append(untieLineEdit->text());
QTextCodec *code1=QTextCodec::codecForName("GB2312");//
QTextStream stream(&file);
stream.setCodec(code1);
stream<<str;
file.close();
this->hide();
}
}
void InterCalate::cancelpushBtn(bool chick){
if(!chick)
this->hide();
}
阅读更多
文章标签: QT
上一篇Qt写入txt文件
下一篇QT之write and read
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭