user352258
user352258

Reputation: 45

How do I keep a value within QlineEdit or a checkbox to retain it's value after program is closed?

How do I keep the value of a user entered QlineEdit or a checkbox that is checked to keep it's state even after program is closed, in this way, the user can access the contents of that checkbox or lineEdit the next time he starts the program.

Upvotes: 0

Views: 823

Answers (2)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48467

Qt has a dedicated solution for that, which is QSettings:

void MainWindow::saveSettings()
{
    QSettings settings("settings.set", QSettings::NativeFormat);

    // save value from QLineEdit
    QString text = lineEdit->text();
    settings.setValue("text", text);

    // save value of QCheckBox
    settings.setValue("box", (int)checkBox->checkState());
}

void MainWindow::loadSettings()
{
    QSettings settings("settings.set", QSettings::NativeFormat);

    // restore value of QLineEdit
    QString text = settings.value("text", "").toString();
    lineEdit->setText(text);

    // restore value of QCheckBox
    checkBox->setCheckState((Qt::CheckState)settings.value("box", 0).toInt());
}

Upvotes: 1

Igor Pejic
Igor Pejic

Reputation: 3698

Store the value in a file and open the file on the next start of the program and read from it.

Upvotes: 0

Related Questions