C++ (Qt)T QextSettings::readValue( const QString &group, const QString &name, T &defaultValue ){ QextSettings::beginGroup( group ); T myValue = qvariant_cast<T>( QextSettings::value( name, defaultValue ) ); QextSettings::endGroup(); return myValue;}
C++ (Qt)//globals.h enum { GET, SET}; enum { OK, ERR_UNKNOWN_TYPE, ERR_UNKNOWN_MODE,}; typedef struct settings_s { void *val_ptr; int val_type; QString val_name;} settings_t; //Global variablesextern int app_locale;extern int cur_scr_mode;extern QString debug_str;
C++ (Qt)//globals.cpp #include "globals.h" int app_locale = RU;int cur_scr_mode = SCREEN;QString debug_str = "Debug string";
C++ (Qt)//main.cpp #include "globals.h" settings_t settings_arr[] = { (void *)&app_locale, QVariant::Int, "app_locale", (void *)&cur_scr_mode, QVariant::Int, "cur_scr_mode", (void *)&debug_str, QVariant::String, "debug_str", NULL}; //Load and save application settings//---------------------------------------------------------------void MyClass::Settings( int mode ){QSettings settings( "MyProg", "Settings" );settings_t *settings_arr_p = settings_arr; do { assert( SettingVal( &settings, settings_arr_p, mode ) == OK ); settings_arr_p++; } while( settings_arr_p->val_ptr != NULL );} //Get and set application setting//---------------------------------------------------------------int MyClass::SettingVal( QSettings *settings_p, settings_t *settings_arr_p, int mode ){ switch( settings_arr_p->val_type ) { case QVariant::Int: if( mode == GET ) { *(int *)settings_arr_p->val_ptr = settings_p->value(settings_arr_p->val_name, *(int *)settings_arr_p->val_ptr).toInt(); } else if( mode == SET ) { settings_p->setValue(settings_arr_p->val_name, *(int *)settings_arr_p->val_ptr); } else return ERR_UNKNOWN_MODE; break; case QVariant::String: if( mode == GET ) { *(QString *)settings_arr_p->val_ptr = settings_p->value(settings_arr_p->val_name, *(QString *)settings_arr_p->val_ptr).toString(); } else if( mode == SET ) { settings_p->setValue(settings_arr_p->val_name, *(QString *)settings_arr_p->val_ptr); } else return ERR_UNKNOWN_MODE; break; default: return ERR_UNKNOWN_TYPE;// break; } return OK;}