#include <QApplication>#include <QStyle>#include <QFontDialog>#include <QMessageBox>#include <QSettings>#include <QDir>//#include "qmysettings.h"#include "config.h"// QMySettings::QMySettings() #ifdef Q_OS_WIN32 :QSettings (QDir::homePath()+"/Application Data/"+_PROGRAM_NAME+".ini", QSettings::IniFormat) #else :QSettings ( _ORGANIZATION, _PROGRAM_NAME ) #endif { #ifdef Q_OS_WIN32 m_font = QFont( _DEFAULT_FONT_WIN, _DEFAULT_FONTSIZE ); #else m_font = QFont( _DEFAULT_FONT_LINUX, _DEFAULT_FONTSIZE ); #endif QMySettings::loadINI();}// ~ DestructorQMySettings::~QMySettings(){ QMySettings::saveINI();}// load all save value in *ini file or registry windowsvoid QMySettings::loadINI(){ // BEGIN group < _INI_GROUP_OPTION > QMySettings::beginGroup( _INI_GROUP_OPTION ); QString s = QMySettings::value( _INI_VALNAME_SETFONT, m_font.toString()).toString(); m_font.fromString(s); m_style = QMySettings::value( _INI_VALNAME_SETSTYLE, QApplication::style()->objectName()).toString(); QMySettings::endGroup(); // END group < _INI_GROUP_OPTION > // BEGIN group < _INI_GROUP_SQL > QMySettings::beginGroup( _INI_GROUP_SQL ); m_driver = QMySettings::value( _INI_VALNAME_DRNAME, _SQL_DEFAULT_DRNAME ).toString(); m_database = QMySettings::value( _INI_VALNAME_DATABASE, _SQL_DEFAULT_DBNAME ).toString(); m_username = QMySettings::value( _INI_VALNAME_USERNAME, _SQL_DEFAULT_USERNAME ).toString(); m_password = QMySettings::value( _INI_VALNAME_PASSWD, _SQL_DEFAULT_PASSWORD ).toString(); m_hostname = QMySettings::value( _INI_VALNAME_HOSTNAME, _SQL_DEFAULT_HOSTNAME ).toString(); m_port = QMySettings::value( _INI_VALNAME_PORT, _SQL_DEFAULT_PORT ).toInt(); m_qslitedb = QMySettings::value( _INI_VALNAME_SQLITEDB, _SQL_DEFAULT_SQLITEDB ).toBool(); QMySettings::endGroup(); // END group < _INI_GROUP_SQL >}// save all value in *ini file or registry windowsvoid QMySettings::saveINI(){ // BEGIN group < _INI_GROUP_OPTION > QMySettings::beginGroup( _INI_GROUP_OPTION ); QMySettings::setValue( _INI_VALNAME_SETFONT, QApplication::font().toString() ); QMySettings::setValue( _INI_VALNAME_SETSTYLE, QApplication::style()->objectName() ); QMySettings::endGroup(); // END group < _INI_GROUP_OPTION >}// - set driver name in *.ini file or registry windowsvoid QMySettings::setDriverName( const QString &value ){ QMySettings::m_driver .clear(); QMySettings::m_driver.append( value ); QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_DRNAME, value);}// - set database name in *.ini file or registry windowsvoid QMySettings::setDatabaseName(const QString &value){ QMySettings::m_database.clear(); QMySettings::m_database.append( value ); QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_DATABASE, value ); }// - set user name in *.ini file or registry windowsvoid QMySettings::setUsername(const QString &value){ QMySettings::m_username.clear(); QMySettings::m_username.append( value ); QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_USERNAME, value );}// - set password in *.ini file or registry windowsvoid QMySettings::setPassword(const QString &value){ QMySettings::m_password.clear(); QMySettings::m_password.append( value ); QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_PASSWD, value );}// set host name in *.ini file or registry windowsvoid QMySettings::setHostname(const QString &value){ QMySettings::m_hostname.clear(); QMySettings::m_hostname.append( value ); QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_HOSTNAME, value );}// set number port in *.ini file or registry windowsvoid QMySettings::setPort(const int &value){ QMySettings::m_port = value; QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_PORT, value );}// set bool used Sqlite for default in *.ini file or registry windowsvoid QMySettings::setSqliteDB(const bool &value){ QMySettings::m_qslitedb = value; QMySettings::save( _INI_GROUP_SQL, _INI_VALNAME_SQLITEDB, value );}// set geometry Widgets in *.ini file or registry windowsvoid QMySettings::saveWidgetGeometry( QWidget * widget ){ QRect myRect = widget->geometry(); QString myObjectName = widget->objectName(); QMySettings::save( _INI_GROUP_GEOMETRY, myObjectName, myRect ); QMySettings::save(_INI_GROUP_GEOMETRY, myObjectName, widget->saveGeometry());}//void QMySettings::setWidgetGeometry( QWidget * widget ){ QMySettings::beginGroup( _INI_GROUP_GEOMETRY ); QString myObjectName = widget->objectName(); QRect myRect = QMySettings::value( myObjectName, widget->geometry() ).toRect(); QByteArray myByteArray = QMySettings::value( myObjectName, widget->saveGeometry() ).toByteArray(); widget->setGeometry( myRect ); widget->restoreGeometry( myByteArray ); QMySettings::endGroup();}//void QMySettings::saveTreeWidgetColumnWidth( QTreeWidget * TreeWidget ){ for( int i=0; i < TreeWidget->columnCount(); i++) { int myWidth = TreeWidget->columnWidth(i); QString myColumn = QString("%1").arg(i); QString myObjectName = QString("%1.%2.%3") .arg( _INI_GROUP_GEOMETRY ) .arg( TreeWidget->objectName() ) .arg( "Column" ); QMySettings::save( myObjectName, myColumn, myWidth ); } }//void QMySettings::setTreeWidgetColumnWidth( QTreeWidget * TreeWidget ){ for( int i=0; i < TreeWidget->columnCount(); i++) { QString myObjectName = QString("%1.%2.%3") .arg( _INI_GROUP_GEOMETRY ) .arg( TreeWidget->objectName() ) .arg( "Column" ); QMySettings::beginGroup( myObjectName ); QString myColumn = QString("%1").arg(i); int myWidth = QMySettings::value( myColumn, _GEO_DEFAULT_COLUMN_WITH ).toInt(); QMySettings::endGroup(); TreeWidget->setColumnWidth( i, myWidth ); }}//void QMySettings::saveTreeViewColumnWidth( QTreeView * TreeView, const int &columnCount ){ for( int i=0; i < columnCount ; i++) { int myWidth = TreeView->columnWidth(i); QString myColumn = QString("%1").arg(i); QString myObjectName = QString("%1.%2.%3") .arg( _INI_GROUP_GEOMETRY ) .arg( TreeView->objectName() ) .arg( "Column" ); QMySettings::save( myObjectName, myColumn, myWidth ); } }//void QMySettings::setTreeViewColumnWidth( QTreeView * TreeView, const int &columnCount ){ for( int i=0; i < columnCount ; i++) { QString myObjectName = QString("%1.%2.%3") .arg( _INI_GROUP_GEOMETRY ) .arg( TreeView->objectName() ) .arg( "Column" ); QMySettings::beginGroup( myObjectName ); QString myColumn = QString("%1").arg(i); int myWidth = QMySettings::value( myColumn, _GEO_DEFAULT_COLUMN_WITH ).toInt(); QMySettings::endGroup(); TreeView->setColumnWidth( i, myWidth ); }}//template<class T>void QMySettings::save( const QString &group, const QString &name, T saveValue ){ QMySettings::beginGroup( group ); QMySettings::setValue( name, saveValue ); QMySettings::endGroup();}//void QMySettings::setDefault(){ // BEGIN group < _INI_GROUP_OPTION > QMySettings::beginGroup( _INI_GROUP_OPTION ); #ifdef Q_OS_WIN32 QMySettings::setValue( _INI_VALNAME_SETFONT, QFont( _DEFAULT_FONT_WIN, _DEFAULT_FONTSIZE ).toString() ); #else QMySettings::setValue( _INI_VALNAME_SETFONT, QFont( _DEFAULT_FONT_LINUX, _DEFAULT_FONTSIZE ).toString() ); #endif QMySettings::setValue( _INI_VALNAME_SETSTYLE, _DEFAULT_STYLE ); QMySettings::endGroup(); // END group < _INI_GROUP_OPTION > // BEGIN group < _INI_GROUP_SQL > QMySettings::beginGroup( _INI_GROUP_SQL ); QMySettings::setValue( _INI_VALNAME_DRNAME, _SQL_DEFAULT_DRNAME ); QMySettings::setValue( _INI_VALNAME_DATABASE, _SQL_DEFAULT_DBNAME ); QMySettings::setValue( _INI_VALNAME_USERNAME, _SQL_DEFAULT_USERNAME ); QMySettings::setValue( _INI_VALNAME_PASSWD, _SQL_DEFAULT_PASSWORD ); QMySettings::setValue( _INI_VALNAME_HOSTNAME, _SQL_DEFAULT_HOSTNAME ); QMySettings::setValue( _INI_VALNAME_PORT, _SQL_DEFAULT_PORT ); QMySettings::setValue( _INI_VALNAME_SQLITEDB, _SQL_DEFAULT_SQLITEDB ); QMySettings::endGroup(); // END group < _INI_GROUP_SQL >}// ========
QSettings (QDir::homePath()+"/Application Data/"+_PROGRAM_NAME+".ini", QSettings::IniFormat)
QSettings settings(QSettings::IniFormat, QSettings::UserScope, "MySoft", "PROGRAM_NAME");
QSettings(QLatin1String("config.ini"),QSettings::IniFormat);