Qt ini read/write

Source: Internet
Author: User

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 =NewQsettings ("HKEY_CURRENT_USER \ Software \ yinhaifan",
    2. Qsettings: nativeformat );
    3. Reg-> setvalue ("Registered",True);
    4. DeleteReg;

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 =NewQsettings ("D: \ A. ini", Qsettings: iniformat, 0 );
    2. Configini-> setvalue ("/Config/node1", UI-> lineedit-> text ());
    3. DeleteConfigini;

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 =NewQsettings ("D: \ A. ini", Qsettings: iniformat, 0 );
    2. UI-> lineedit_2-> settext (configini-> value ("/Config/node1","20"). Tostring ());
    3. DeleteConfigini;

Qsettings * configini = new qsettings ("D: \. ini ", qsettings: iniformat, 0); UI-> lineedit_2-> settext (configini-> value ("/config/node1 "," 20 "). tostring (); Delete configini;

These paragraphsCodeThey are all written in QT creator, so we use UI-> lineedit *-> text () and UI-> lineedit *-> settext () for 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 ();

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.