关闭

QT写入文本文件

标签: QT
116人阅读 评论(0) 收藏 举报
#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();
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3374次
    • 积分:151
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章存档