Search code examples
windowsqtsavesettingsrestore

How To Save Settings in Qt


I wrote some codes for an application and I want to save this Settings Like Hide a lineEdit or etc... and when reopen program last settings will load and when user edit setting settings that saved updates

what I must do?

note: I Used Qsettings but settings dose not saved! if Possible one person Write a Sample Code For me That save current index of a combobox

QSettings settings("Mobtakeran Fanavri KabooK","Kabook Physiothrapy");

Secretary::Secretary(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Secretary)
{
    ui->setupUi(this);
    ui->comboBox->setCurrentIndex(settings.value("comboBox").toInt());
}
Secretary::~Secretary()
{
    QCoreApplication::setOrganizationName("Mobtakeran Fanavri KabooK");
    QCoreApplication::setOrganizationName("WWW.M4RZB4Ni.IR");
    QCoreApplication::setApplicationName("Kabook Physiothrapy");

    delete ui;
}
void Secretary::on_comboBox_currentIndexChanged(int index)
{
    settings.beginGroup("comboBox");
    if(ui->comboBox->currentIndex()==2) {
        ui->pushButton_3->setDisabled(true);
    } else if(ui->comboBox->currentIndex()==1) {
        ui->pushButton_3->hide();
        settings.setValue("comboBox",ui->comboBox->currentIndex());
    } else if(ui->comboBox->currentIndex()==0) {
        if(ui->lineEdit_56->text()==NULL) {
           ui->pushButton_8->setDisabled(true);
        }
    }
    settings.endGroup();
}

Solution

  • when you are saving your settings in Secretary::on_comboBox_currentIndexChanged you are calling settings.beginGroup("comboBox") then you set the value settings.setValue("comboBox",ui->comboBox->currentIndex()).

    According to the documentation, this will set the value of the settings "comboBox/comboBox", meaning that you should read its value using settings.value("comboBox/comboBox").toInt().

    Also please note that you are calling settings.setValue only in the case where currentIndex changes to 2, are you sure you mean to do that? didn't you mean to call it after all your if/else blocks?