1 //Settings.h 2 #ifndef _SETTINGS_H 3 #define _SETTINGS_H 4 5 class Settings 6 { 7 public: 8 Settings(); 9 10 int load(); 11 int save(); 12 13 public: 14 int period; 15 int radius; 16 int grain; 17 }; 18 #endif
1 //Settings.cpp 2 #include "Settings.h" 3 #include "../tinyxml/AfTinyXml.h" 4 5 Settings::Settings() 6 { 7 8 } 9 10 int Settings::load() 11 { 12 // 解析xml 13 TiXmlDocument xml_doc; 14 if(!xml_doc.LoadFile("settings.xml")) 15 { 16 return -1; 17 } 18 19 // 根節點 20 TiXmlElement* xml_root = xml_doc.RootElement(); 21 if (NULL == xml_root) 22 { 23 return -1; 24 } 25 26 this->period = AfTinyXml::childAsInt(xml_root, "period"); 27 this->radius = AfTinyXml::childAsInt(xml_root, "radius"); 28 this->grain = AfTinyXml::childAsInt(xml_root, "grain"); 29 return 0; 30 } 31 32 int Settings::save() //保存配置文件 33 { 34 TiXmlDocument xml_doc; 35 xml_doc.LinkEndChild(new TiXmlDeclaration( "1.0", "GBK", "" )); 36 37 TiXmlElement * xml_root = new TiXmlElement("root"); 38 xml_doc.LinkEndChild(xml_root); 39 40 AfTinyXml::addChild(xml_root, "period", period); 41 AfTinyXml::addChild(xml_root, "radius", radius); 42 AfTinyXml::addChild(xml_root, "grain", grain); 43 44 xml_doc.SaveFile("settings.xml"); 45 return 0; 46 }
1 //qt.h 2 #ifndef QT5_H 3 #define QT5.H 4 5 #include <QtGui/QWidget> 6 #include "ui_QT5.h" 7 8 #include "Settings.h" 9 10 class QT5 : public QWidget 11 { 12 Q_OBJECT 13 14 public: 15 QT5(QWidget *parent = 0, Qt::WFlags flags = 0); 16 ~QT5(); 17 18 private slots: 19 int OnSettingsChanged(); 20 21 private: 22 Ui::QT5Class ui; 23 24 Settings m_settings;//聲明一個Settings類實例m_settings 25 }; 26 27 #endif // QT5
1 //qt5.cpp 2 #include "qt5.h" 3 4 QT5::QT5(QWidget *parent, Qt::WFlags flags) 5 : QWidget(parent, flags) 6 { 7 ui.setupUi(this); 8 9 m_settings.load();//讀取配置文件 10 ui.m_ctlPeriod->setValue(m_settings.period); 11 ui.m_ctlGrain->setValue(m_settings.grain); 12 ui.m_ctlRadius->setValue(m_settings.radius); 13 14 connect(ui.m_ctlRadius, SIGNAL( valueChanged(int)), this, SLOT(OnSettingsChanged())); 15 connect(ui.m_ctlGrain, SIGNAL( valueChanged(int)), this, SLOT(OnSettingsChanged())); 16 connect(ui.m_ctlPeriod, SIGNAL( valueChanged(int)), this, SLOT(OnSettingsChanged())); 17 18 ui.frame->Adjust(m_settings.period, m_settings.grain, m_settings.radius); 19 20 } 21 22 QT5::~QT5() 23 { 24 25 } 26 27 28 int QT5::OnSettingsChanged() 29 { 30 ui.frame->Adjust(ui.m_ctlPeriod->value(), 31 ui.m_ctlGrain->value(), 32 ui.m_ctlRadius->value()); 33 34 m_settings.period = ui.m_ctlPeriod->value(); 35 m_settings.grain = ui.m_ctlGrain->value(); 36 m_settings.radius = ui.m_ctlRadius->value(); 37 m_settings.save(); //保存配置文件 38 39 return 0; 40 }