此处以QRadioButton、QLineEdit为例子,其它qt组件的状态保存配置文件方法举一反三;
实现代码如下:
需要的头文件:
#include <qsettings.h>
一、储存配置信息
//创建一个QSetting对象储存配置文件
QSettings settings("config.ini",QSetting::Iniformat);
//将QRadioButton组件信息写入.ini
settings.setValue("radioButton",ui->radioButton->isChecked());
settings.setValue("radioButton_2",ui->radioButton_2->isChecked());
//将QLineEdit组件信息写入.ini
QString text = ui->lineEdit->text(); //获取QLineEdit的文本内容
settings.setValue("lineEdit",text);
text = ui->lineEdit_2->text(); //获取QLineEdit的文本内容
settings.setValue("lineEdit_2",text);
文章来源:https://uudwc.com/A/ZGYOE
二、读取配置信息
QSetting settings("config.ini",QSettings::IniFormat);
//读取组件状态信息
//设置QRadioButton状态
bool IsChecked = settings.value("radioButton",false).toBool();
ui->radioButton->setChecked(IsChecked);
IsChecked = settings.value("radioButton_2",false).toBool();
ui->radioButton_2->setChecked(IsChecked);
//设置QLineEdit内容
QString text = settings.value("lineEdit","").toString();
ui->lineEdit->setText(text);
text = settings.value("lineEdit_2","").toString();
ui->lineEdit_2->setText(text);
文章来源地址https://uudwc.com/A/ZGYOE