当前位置:   article > 正文

Qt之ini配置文件的读写_qt 读取编辑保存ini文件

qt 读取编辑保存ini文件

由于开发过程中有用到ini的操作较频繁,故作此记录

  1. // 保存(制作)配置文件
  2. void MainWindow::saveConfig()
  3. {
  4. QString path = QCoreApplication::applicationDirPath();
  5. qDebug()<<path;
  6. QString iniPath = path + "/myTest.ini";
  7. QFileInfo file_info(iniPath);
  8. if(!file_info.isFile()){
  9. QMessageBox::information(this,"提示","文件不存在,将自动创建");
  10. }
  11. QSettings setting(iniPath,QSettings::IniFormat);
  12. // 方式一
  13. setting.setValue("/baseInformation/ID",ui->lineEdit->text());
  14. setting.setValue("/baseInformation/name",ui->lineEdit_2->text());
  15. setting.setValue("/baseInformation/age",ui->lineEdit_3->text());
  16. for(int i = 0;i < 10;i++){
  17. setting.setValue(QString("/GRADES/grades_%1").arg(i),rand()%100+2);
  18. }
  19. /*// 方式二
  20. setting.beginGroup("baseInformation");
  21. setting.setValue("ID",ui->lineEdit->text());
  22. setting.setValue("name",ui->lineEdit_2->text());
  23. setting.setValue("age",ui->lineEdit_3->text());
  24. setting.endGroup();
  25. setting.beginGroup("GRADES");
  26. for(int i = 0;i < 10;i++){
  27. setting.setValue(QString("grades_%1").arg(i),QString("%1").arg(rand()%14+1));
  28. }
  29. setting.endGroup();*/
  30. QMessageBox::information(this,"提示","保存成功!");
  31. }
  1. // 读取配置文件
  2. void MainWindow::readConfig()
  3. {
  4. QString path = QCoreApplication::applicationDirPath();
  5. QString iniPath = path + "/myTest.ini";
  6. QFileInfo file_info(iniPath);
  7. if(!file_info.isFile()){
  8. QMessageBox::information(this,"提示","配置文件不存在!");
  9. return; // 返回,不进行下面的操作
  10. }
  11. QSettings setting(iniPath,QSettings::IniFormat);
  12. // 读取方式一
  13. QString ID = setting.value("/baseInformation/ID").toString();
  14. QString name = setting.value("/baseInformation/name").toString();
  15. QString age = setting.value("/baseInformation/age").toString();
  16. /*// 读取方式二
  17. setting.beginGroup("baseInformation");
  18. QString ID = setting.value("ID","").toString();
  19. QString name = setting.value("name","").toString();
  20. QString age = setting.value("age","").toString();
  21. setting.endGroup();*/
  22. ui->lineEdit->setText(ID);
  23. ui->lineEdit_2->setText(name);
  24. ui->lineEdit_3->setText(age);
  25. }

参考文章:Qt读写ini配置文件_qt读ini配置文件_三雷科技的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/300278
推荐阅读
相关标签
  

闽ICP备14008679号