pseudo codeQString text = label->getText();saveSettings("myLabel", text);text = TextEdit->getText();saveSettings("myTextEdit", text);-----------------------restoreSettings(){ QString text = load("label"); label->setText(text); text = load("TextEdit"); textEdit->setText(text);}
C++ (Qt)lineEdit->setProperty("getter", "text");lineEdit->setProperty("setter", "setText");
C++ (Qt)QVariant retVal;QMetaObject::invokeMethod(widget, widget->property("getter"), Q_RETURN_ARG(QVariant, retVal));// сохранить retVal
C++ (Qt)// считываешь значение в QVariant valueQMetaObject::invokeMethod(widget, widget->property("setter"), Q_ARG(QVariant, value));
C++ (Qt)QList<QWidget *> widgets = findChildren<QWidget *>();for (int i = 0, count = widgets.count(); i < count; ++i){ QWidget * w = widgets[i]; QVariant v; if (w->metaObject()->className() == "QLineEdit") { QLineEdit * le = qobject_cast<QLineEdit *>(w); if(le) { v = le->text(); } } else if (w->metaObject()->className() == "QCheckBox") { QCheckBox *cb = qobject_cast<QCheckBox *>(w); if(cb) v = cb->isChecked(); } //....}
void SaveSettings(QWidget * widget, QString organization,QString application, const char * AddPropName){ QSettings settings(organization,application); settings.beginGroup(widget->objectName()); settings.setValue("size", widget->size()); settings.setValue("pos", widget->pos()); foreach (QWidget * w, widget->findChildren<QWidget *>()) { if (w->property(AddPropName).toBool()) //Need for save/load { if (QLineEdit * o = qobject_cast<QLineEdit *>(w)) { settings.setValue(o->objectName(),o->text()); continue; } if (QDateEdit * o = qobject_cast<QDateEdit *>(w)) { settings.setValue(o->objectName(),o->date()); continue; } if (QTimeEdit * o = qobject_cast<QTimeEdit *>(w)) { settings.setValue(o->objectName(),o->time()); continue; } if (QRadioButton * o = qobject_cast<QRadioButton *>(w)) { settings.setValue(o->objectName(),o->isChecked()); continue; } if (QCheckBox * o = qobject_cast<QCheckBox *>(w)) { settings.setValue(o->objectName(),o->isChecked()); continue; } } } settings.endGroup();}
SaveSettings(this,organization,application,"Save");
pseudo codeclass ISavingWidget{ static const QString organization; static const QString application; bool needSave; virtual bool saveSettings() { реализация по умолчанию; } virtual bool loadSettings() { реализация по умолчанию; }};claass MyWidget : public ISaveWidget { private: QLineEdit *e; QDateTime *t; ---- public: bool saveSettings() { if ( !needSave ) return false; // нечего сохранять то что не надо сохранять, поэтому false QSettings s(org, app); .... s.setValue(e->objectName(), e->text); .... } bool loadSettings() { ... }};void saveSettings(ISaveWidget *w){ if ( w->saveSettings() ) { LOG << "...." } else { .... }}void loadSettings(ISaveWidget *w){ if ( w->loadSettings() ) { ... } else ....}void main(){ MyWidget w; saveSettings(&w); --- если надо расширить функционал //или просто w.saveSettings}
C++ (Qt)bool saveSettings(const QObject * obj, QSettings *settings){ if(!(obj && settings)) { return false; } QString objectName_(obj->objectName()); if(objectName_.isEmpty()) { qWarning("objectName is empty"); return false; } settings->beginGroup(objectName_); const QMetaObject * metaObj_ = obj->metaObject(); int count = metaObj_->propertyCount(); for(int i = 0; i < count; ++i) { QMetaProperty prop = metaObj_->property(i); settings->setValue(QString::fromLatin1(prop.name()), prop.read(obj)); } settings->endGroup(); return true;}
C++ (Qt)bool restoreSettings(QObject * obj, QSettings *settings){ if(!(obj && settings)) { return false; } QString objectName_(obj->objectName()); if(objectName_.isEmpty()) { qWarning("objectName is empty"); return false; } settings->beginGroup(objectName_); const QMetaObject * metaObj_ = obj->metaObject(); int count = metaObj_->propertyCount(); for(int i = 0; i < count; ++i) { QMetaProperty prop = metaObj_->property(i); prop.write(obj, settings->value(QString::fromLatin1(prop.name()))); } settings->endGroup(); return true;}