美文网首页
Qt | How to load/save .ini File

Qt | How to load/save .ini File

作者: Machin_Yin | 来源:发表于2017-08-17 13:22 被阅读0次

    本文作为个人笔记用。文章出处:http://ohurochan.jp/blog/en/development/qt-how-to-loadsave-ini-file-with-qt

    This note is written for setting/getting parameter of App to/from “.ini” file.

    [h1]sample.ini

    # this is a Sample ini file
     [GROUPA]
     PARAM1 = 10
     PARAM2 = 20
    
     [GROUPB]
     PARAM1 = 30
     PARAM2 = 40
     PARAM3 = ABC
    

    [h1] how to load params from .ini

            // Load Application params from ini file
            QSettings settings( iniFileName, QSettings::IniFormat );
    
            settings.beginGroup("GROUPA");
            int param1 = settings.value("PARAM1").toInt();
            int param2 = settings.value("PARAM2").toInt();
            settings.endGroup();
    
            settings.beginGroup("GROUPB");
            int param1 = settings.value("PARAM1").toInt();
            int param2 = settings.value("PARAM2").toInt();
            int param2 = settings.value("PARAM3").toString();
            settings.endGroup();
    

    This code will get

    GROUPA.param1 = 10
    GROUPA.param2 = 20
    GROUPB.param1 = 30
    GROUPB.param2 = 40
    GROUPB.param3 = ABC
    

    Get all sections

        QSettings settings( "sample.ini", QSettings::IniFormat );
        QStringList keys = settings.childGroups();
        qDebug() << "keys == " << keys;
    

    This code will get

    keys ==  ("GROUPA", "GROUPB")
    

    Delete group

        settings.remove(keys.at(1));
        keys = settings.childGroups();
        qDebug() << "keys == " << keys;
    

    This code will get

    keys ==  ("GROUPA")
    

    The sample.ini will become

    # this is a Sample ini file
     [GROUPA]
     PARAM1 = 10
     PARAM2 = 20
    

    [h1] how to save params to .ini

            // Load Application params from ini file
            QSettings settings( iniFileName, QSettings::IniFormat );
    
            settings.beginGroup("GROUPA");
            settings.setValue("PARAM1", 10);
            settings.setValue("PARAM2", 20);
            settings.endGroup();
    
            settings.beginGroup("GROUPB");
            settings.setValue("PARAM1", 30);
            settings.setValue("PARAM2", 40);
            settings.setValue("PARAM3", "ABC");
            settings.endGroup();
    

    相关文章

      网友评论

          本文标题:Qt | How to load/save .ini File

          本文链接:https://www.haomeiwen.com/subject/nssgrxtx.html