1, ini文件在windows系统中可以存储需要持久保存的配置信息,注册表信息等;文件格式由键 值组成,键值由section分成不同的段,如下图,[GPS]为section name,gpsBaud为键,4800为值。Qt 的QSettings类可很容易实现ini文件的读写
2,新建QtWidget工程
3,INI写(需包含QSettings)
void MainWindow::on_writepushButton_clicked() { QSettings settings("TestSetting.ini", QSettings::IniFormat); // 当前目录的INI文件 //GPS setting settings.beginGroup("GPS"); settings.setValue("gpsBaud", 4800); settings.setValue("gpsPort", "com7"); settings.setValue("gpsType", "uBlox"); settings.endGroup(); }
单击writeIni按钮,在当前目录中生成TestSetting.ini文件
TestSetting.ini文件内容如下:
4,INI读
void MainWindow::on_readpushButton_clicked() { QSettings settings("TestSetting.ini", QSettings::IniFormat); if(settings.contains("GPS/gpsBaud")) { QString gpsBaud = settings.value("GPS/gpsBaud").toString(); ui->gpsBaudlineEdit->clear(); ui->gpsBaudlineEdit->insert(gpsBaud); QString gpsPort = settings.value("GPS/gpsPort").toString(); ui->gpsPortlineEdit->clear(); ui->gpsPortlineEdit->insert(gpsPort); QString gpsType = settings.value("GPS/gpsType").toString(); ui->gpsTypelineEdit->clear(); ui->gpsTypelineEdit->insert(gpsType); } }
5,结果如下: