For a couple of checks/radio buttons this is probably the most obvious way. For a larger number you could make use of the QObject object name to hold the name of the settings key for the check box and then use that in a single servicing slot. Here is an example with generated keys but you can set them manually (in Designer for example):
#include <QtGui>
class LotsOfChecks
: public QWidget { Q_OBJECT
public:
setLayout(layout);
for (int i = 0; i < 10; ++i) {
cb
->setObjectName
(QString("Check_%1").
arg(i
));
connect(cb, SIGNAL(toggled(bool)), SLOT(storeCheck(bool)));
layout->addWidget(cb);
}
restoreChecks();
}
private:
void restoreChecks() {
foreach
(const QString &key, settings.
childKeys()) { if (cb)
cb->setChecked(settings.value(key).toBool());
}
}
private slots:
void storeCheck(bool checked) {
QString key
= sender
()->objectName
();
settings.setValue(key, checked);
}
};
int main(int argc, char **argv)
{
app.setApplicationName("thisapp");
app.setOrganizationName("com.example");
LotsOfChecks w;
w.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
class LotsOfChecks: public QWidget {
Q_OBJECT
public:
LotsOfChecks(QWidget *p = 0): QWidget(p) {
QVBoxLayout *layout = new QVBoxLayout(this);
setLayout(layout);
for (int i = 0; i < 10; ++i) {
QCheckBox *cb = new QCheckBox("Some cool label", this);
cb->setObjectName(QString("Check_%1").arg(i));
connect(cb, SIGNAL(toggled(bool)), SLOT(storeCheck(bool)));
layout->addWidget(cb);
}
restoreChecks();
}
private:
void restoreChecks() {
QSettings settings;
foreach(const QString &key, settings.childKeys()) {
QCheckBox *cb = findChild<QCheckBox*>(key);
if (cb)
cb->setChecked(settings.value(key).toBool());
}
}
private slots:
void storeCheck(bool checked) {
QString key = sender()->objectName();
QSettings settings;
settings.setValue(key, checked);
}
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
app.setApplicationName("thisapp");
app.setOrganizationName("com.example");
LotsOfChecks w;
w.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks