赞
踩
由于开发过程中有用到ini的操作较频繁,故作此记录
- // 保存(制作)配置文件
- void MainWindow::saveConfig()
- {
- QString path = QCoreApplication::applicationDirPath();
- qDebug()<<path;
- QString iniPath = path + "/myTest.ini";
- QFileInfo file_info(iniPath);
- if(!file_info.isFile()){
- QMessageBox::information(this,"提示","文件不存在,将自动创建");
- }
-
- QSettings setting(iniPath,QSettings::IniFormat);
-
- // 方式一
- setting.setValue("/baseInformation/ID",ui->lineEdit->text());
- setting.setValue("/baseInformation/name",ui->lineEdit_2->text());
- setting.setValue("/baseInformation/age",ui->lineEdit_3->text());
- for(int i = 0;i < 10;i++){
- setting.setValue(QString("/GRADES/grades_%1").arg(i),rand()%100+2);
- }
- /*// 方式二
- setting.beginGroup("baseInformation");
- setting.setValue("ID",ui->lineEdit->text());
- setting.setValue("name",ui->lineEdit_2->text());
- setting.setValue("age",ui->lineEdit_3->text());
- setting.endGroup();
-
- setting.beginGroup("GRADES");
- for(int i = 0;i < 10;i++){
- setting.setValue(QString("grades_%1").arg(i),QString("%1").arg(rand()%14+1));
- }
- setting.endGroup();*/
-
- QMessageBox::information(this,"提示","保存成功!");
- }
- // 读取配置文件
- void MainWindow::readConfig()
- {
- QString path = QCoreApplication::applicationDirPath();
- QString iniPath = path + "/myTest.ini";
- QFileInfo file_info(iniPath);
- if(!file_info.isFile()){
- QMessageBox::information(this,"提示","配置文件不存在!");
- return; // 返回,不进行下面的操作
- }
-
- QSettings setting(iniPath,QSettings::IniFormat);
- // 读取方式一
- QString ID = setting.value("/baseInformation/ID").toString();
- QString name = setting.value("/baseInformation/name").toString();
- QString age = setting.value("/baseInformation/age").toString();
-
- /*// 读取方式二
- setting.beginGroup("baseInformation");
- QString ID = setting.value("ID","").toString();
- QString name = setting.value("name","").toString();
- QString age = setting.value("age","").toString();
- setting.endGroup();*/
-
-
- ui->lineEdit->setText(ID);
- ui->lineEdit_2->setText(name);
- ui->lineEdit_3->setText(age);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。