Qsettings * configini = new qsettings (qsettings: INI ); Configini-> insertsearchpath (qsettings: Unix, "/home/Dawei");. // configuration file directory Configini-> writeentry (qstring: fromutf8 ("/config./opt/parameter 1"), qstring: fromutf8 ("Chinese ")); String AAA = configini-> readentry (qstring: fromutf8 ("/config./opt/parameter 1 "); Delete configini; The config. RC configuration file is generated in the/home/Dawei directory. [Opt] Parameter 1 = Chinese --------------------------------------- Qsettings settings ("../etc/BPMs. conf", qsettings: iniformat, & App ); Qstring db_hostname = settings. Value ("hostname"). tostring (); Qstring db_database = settings. Value ("Database"). tostring (); Qstring db_username = settings. Value ("username"). tostring (); Qstring db_password = settings. Value ("password"). tostring (); Qsqldatabase DB = qsqldatabase: adddatabase ("qmysql "); DB. sethostname (db_hostname ); DB. setdatabasename (db_database ); DB. setusername (db_username ); DB. setpassword (db_password ); --------------------------------------------------------------- # Include <qsettings> Here are just a few simple examples: 1. Use qsettings to write the Registry
- Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan ",
- Qsettings: nativeformat );
- Reg-> setvalue ("registered", true );
- Delete reg;
Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan", qsettings: nativeformat); reg-> setvalue ("registered", true); Delete reg; Of course, reg-> setvalue can also write data such as strings and integers. For example, reg-> setvalue ("registered", "yes "); 2. Use qstring to read the Registry
- Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan ",
- Qsettings: nativeformat );
- If (reg-> value ("registered", false). tobool ())
- {
- Qmessagebox: Information (null, "information", "registered ");
- }
- Delete reg;
Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan", qsettings: nativeformat); If (reg-> value ("registered", false ). tobool () {qmessagebox: Information (null, "information", "registered");} Delete reg; You can also use toint (), tostring (), and so on. 3. Use qsettings to write the INI file:
- Qsettings * configini = new qsettings ("D: // A. ini", qsettings: iniformat, 0 );
- Configini-> setvalue ("/config/node1", UI-> lineedit-> text ());
- Delete configini;
Qsettings * configini = new qsettings ("D: //. ini ", qsettings: iniformat, 0); configini-> setvalue ("/config/node1 ", UI-> lineedit-> text (); Delete configini; 4. Use qsettings to read the INI file:
- Qsettings * configini = new qsettings ("D: // A. ini", qsettings: iniformat, 0 );
- UI-> lineedit_2-> settext (configini-> value ("/config/node1", "20"). tostring ());
- Delete configini;
Qsettings * configini = new qsettings ("D: //. ini ", qsettings: iniformat, 0); UI-> lineedit_2-> settext (configini-> value ("/config/node1 "," 20 "). tostring (); Delete configini; These codes are written in QT creator, so UI-> lineedit *-> text () and UI-> lineedit *-> settext () are used () input and output. The qsettings class reference section in this document is clear. --------------------------------------------------------------------- M_ndevno = ndevno; Qstring strtemp; Qstring xmlpath = csc_app.m_strworkpath + "/initinfo. ini "; Qsettings * mysetting = new qsettings (xmlpath, qsettings: iniformat ); Foreach (qstring group, mysetting-> childgroups ()) { Mysetting-> begingroup (group ); Foreach (qstring key, mysetting-> childkeys ()) { Qtextcodec * codec = qtextcodec: codecforname ("GBK "); Qstring outstr = codec-> tounicode (key. tolatin1 ()); If (outstr = "self-description language ") { If (mysetting-> value (key, "0"). toint () = 1) Ui. selfdsplgu-> setcurrentindex (1 ); Else Ui. selfdsplgu-> setcurrentindex (0 ); } If (outstr = "working language ") { If (mysetting-> value (key, "0"). toint () = 1) Ui. worklgu-> setcurrentindex (1 ); Else Ui. worklgu-> setcurrentindex (0 ); } If (outstr = "working offline ") { If (mysetting-> value (key, "0"). toint () = 1) Ui. workmode-> setcurrentindex (1 ); Else Ui. workmode-> setcurrentindex (0 ); } If (outstr = "call self-description ") { If (mysetting-> value (key, "0"). toint () = 1) Ui. selfdspsource-> setcurrentindex (1 ); Else Ui. selfdspsource-> setcurrentindex (0 ); } If (outstr = "multicast address ") { Strtemp = mysetting-> value (key, "236.8.8.8"). tostring (); Ui. Gr address-> settext (strtemp ); } If (outstr = "receive port number ") { Strtemp = mysetting-> value ("receiving port number", "2000"). tostring (); Ui. recport-> settext (strtemp ); } If (outstr = "sending port number ") { Strtemp = mysetting-> value ("sending port number", "2001"). tostring (); Ui. targetport-> settext (strtemp ); } If (outstr = "enable local address ") { Strtemp = mysetting-> value (key, "0"). tostring (); } If (outstr = "local address ") { Strtemp = mysetting-> value (key, "0"). tostring (); Ui. localip-> settext (strtemp ); } If (outstr = "device name ") { Strtemp = mysetting-> value (key, "0"). tostring (); Ui. eqpname-> settext (strtemp ); } If (outstr = "device address ") { Strtemp = mysetting-> value (key, "0"). tostring (); Ui. eqpaddress-> settext (strtemp ); } If (outstr = "device IP ") { Strtemp = mysetting-> value (key, "0"). tostring (); Ui. targetip-> settext (strtemp ); } } Mysetting-> endgroup (); This article is from "We 23, 4 this year" blog, please be sure to keep this source http://no001.blog.51cto.com/1142339/383314 |