打开方式:
1 void AddStudents::write_to_file(QString src){ 2 QFile file("stu.txt"); 3 if (!file.open(QIODevice::Append | QIODevice::Text)){ 4 QMessageBox::critical(this,"打开文件错误","确认"); 5 return; 6 } 7 QTextStream out(&file); 8 out << src; 9 10 }
1 if(context.length() != 0 && name != "" && id !="" && ins.length()!= 0){ 2 int res = QMessageBox::information(this,"请确认信息",context,"确认","取消"); 3 if(res == 0){ 4 //点击确定 5 //由于准备采用readline的方式来读取文件,所以不能是用'\n'来拼接字符串,换成' '符号来拼接 6 QString src_s = name +' ' + id+' '+sex+' '+sche+' '+age+' '+ins; 7 write_to_file(src_s); 8 //恢复添加界面属性值为初始值 9 clear_on_addstudent_ok(); 10 } 11 }else{ 12 QMessageBox::critical(this,"请确认信息","信息不完整,请重新输入!","确认","取消"); 13 //重新设置光标位置。这里设置name对应的lineEdit控件获取光标 14 this->ui->lineedit_name->setFocus(); 15 }