参考:https://zhuanlan.zhihu.com/p/351214526
介绍
在产品使用过程中,有些用户有特定的习惯。比如窗口大小是的大小,文件打开的方式等等。
因此我们需要有一个文件来保存用户的一些偏好信息,在程序重新启动的时候再进行偏好设置。
这里用到了QSetting类。
这个类允许你创建一个配置文件或者操作注册表,你还可以指定配置文件的位置,当然,读取也是没问题的。
头文件
#include <QSettings>
使用方式
首先定义一个Qsetting指针确定路径
QString settingPath = QCoreApplication::applicationDirPath() + "/setting.ini";
QSettings *settings = new QSettings(settingPath,QSettings::IniFormat);
然后在一个Group里使用setValue,就可以写入到文件,示例如下
settings->beginGroup("PortA");
settings->setValue("COM",ui->comboBox_com->currentText());
settings->setValue("Baud",ui->comboBox_baud->currentText());
settings->endGroup();
读文件使用Value,示例如下:
settings->beginGroup("PortA");
settings->value("COM").toString()
settings->endGroup();
记得不管是保存,还是读取,用完后要释放指针
delete settings;
保存到文件例子:
示例:比如在析构函数中写入配置文件文章来源:https://www.toymoban.com/news/detail-634645.html
PortA::~PortA()
{
//保存关闭时的状态
QString settingPath = QCoreApplication::applicationDirPath() + "/setting.ini";
QSettings *settings = new QSettings(settingPath,QSettings::IniFormat);
settings->beginGroup("PortA");
settings->setValue("COM",ui->comboBox_com->currentText());
settings->setValue("Baud",ui->comboBox_baud->currentText());
settings->setValue("Parity",ui->comboBox_parity->currentText());
settings->setValue("Databit",ui->comboBox_databit->currentText());
settings->setValue("Stopbit",ui->comboBox_stopbit->currentText());
settings->setValue("str0",ui->lineEdit_ex0->text());
settings->setValue("str1",ui->lineEdit_ex1->text());
settings->setValue("str2",ui->lineEdit_ex2->text());
settings->setValue("str3",ui->lineEdit_ex3->text());
settings->setValue("str4",ui->lineEdit_ex4->text());
settings->setValue("str5",ui->lineEdit_ex5->text());
settings->setValue("str6",ui->lineEdit_ex6->text());
settings->setValue("str7",ui->lineEdit_ex7->text());
settings->setValue("hex_ex0",ui->checkBox_ex0->isChecked());
settings->setValue("hex_ex1",ui->checkBox_ex1->isChecked());
settings->setValue("hex_ex2",ui->checkBox_ex2->isChecked());
settings->setValue("hex_ex3",ui->checkBox_ex3->isChecked());
settings->setValue("hex_ex4",ui->checkBox_ex4->isChecked());
settings->setValue("hex_ex5",ui->checkBox_ex5->isChecked());
settings->setValue("hex_ex6",ui->checkBox_ex6->isChecked());
settings->setValue("hex_ex7",ui->checkBox_ex7->isChecked());
settings->setValue("protocol_filename",protocol_filename);
settings->endGroup();
delete settings;
delete ui;
}
读取配置文件示例:
比如在构造函数中读取配置文件:文章来源地址https://www.toymoban.com/news/detail-634645.html
PortA::PortA(QWidget *parent) :
QWidget(parent),
ui(new Ui::PortA)
{
ui->setupUi(this);
//读取上次关闭时的状态
QString settingPath = QCoreApplication::applicationDirPath() + "/setting.ini";
QSettings *settings = new QSettings(settingPath,QSettings::IniFormat);
settings->beginGroup("PortA");
ui->comboBox_com->setCurrentText(settings->value("COM").toString());
ui->comboBox_baud->setCurrentText(settings->value("Baud").toString());
ui->comboBox_parity->setCurrentText(settings->value("Parity").toString());
ui->comboBox_databit->setCurrentText(settings->value("Databit").toString());
ui->comboBox_stopbit->setCurrentText(settings->value("Stopbit").toString());
ui->lineEdit_ex0->setText(settings->value("str0").toString());
ui->lineEdit_ex1->setText(settings->value("str1").toString());
ui->lineEdit_ex2->setText(settings->value("str2").toString());
ui->lineEdit_ex3->setText(settings->value("str3").toString());
ui->lineEdit_ex4->setText(settings->value("str4").toString());
ui->lineEdit_ex5->setText(settings->value("str5").toString());
ui->lineEdit_ex6->setText(settings->value("str6").toString());
ui->lineEdit_ex7->setText(settings->value("str7").toString());
ui->checkBox_ex0->setChecked(settings->value("hex_ex0").toBool());
ui->checkBox_ex1->setChecked(settings->value("hex_ex1").toBool());
ui->checkBox_ex2->setChecked(settings->value("hex_ex2").toBool());
ui->checkBox_ex3->setChecked(settings->value("hex_ex3").toBool());
ui->checkBox_ex4->setChecked(settings->value("hex_ex4").toBool());
ui->checkBox_ex5->setChecked(settings->value("hex_ex5").toBool());
ui->checkBox_ex6->setChecked(settings->value("hex_ex6").toBool());
ui->checkBox_ex7->setChecked(settings->value("hex_ex7").toBool());
settings->endGroup();
delete settings;
}
到了这里,关于QT使用QSetting保存程序运行时的一些状态信息到配置文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!