Qt reads INI files

Source: Internet
Author: User
Qt reads INI files 2010-08-26 10:53:30Tags: INI file QT [push to technical circle]
Copyright:Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://no001.blog.51cto.com/1142339/383314

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

  1. Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan ",
  2. Qsettings: nativeformat );
  3. Reg-> setvalue ("registered", true );
  4. 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

  1. Qsettings * Reg = new qsettings ("HKEY_CURRENT_USER // software // yinhaifan ",
  2. Qsettings: nativeformat );
  3. If (reg-> value ("registered", false). tobool ())
  4. {
  5. Qmessagebox: Information (null, "information", "registered ");
  6. }
  7. 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:

  1. Qsettings * configini = new qsettings ("D: // A. ini", qsettings: iniformat, 0 );
  2. Configini-> setvalue ("/config/node1", UI-> lineedit-> text ());
  3. 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:

  1. Qsettings * configini = new qsettings ("D: // A. ini", qsettings: iniformat, 0 );
  2. UI-> lineedit_2-> settext (configini-> value ("/config/node1", "20"). tostring ());
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.