Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSettings - get setting value of form x,x|x,x|x,x|x,x| fails



  • I am using QSettings to parse an ini file, which works fine except for one particular entry in the ini file (which I don't control).

    [Key]
    KeyValue=7,10|4,6|3,3|2,2|

    using

    @QSettings settings(cfgPath, QSettings::IniFormat);
    settings.beginGroup("Key");
    QString KeyValue_= settings.value("KeyValue", "").toString();
    qDebug() << KeyValue_ ;@

    but this fails to return the value. All other values of a more traditional form Var = Value. I presume it is something to do with the way QSettings parses this, is there anyway around it?

    Also, the ini file starts with a general section.

    [General]

    The parser returns values from this outside of a beginGroup, but not if I do a setting.beginGroup("General") ?

    e.g.

    Works
    @QString regCode_ = settings.value("RegistrationCode", "").toString();
    qDebug() << regCode_ ;@

    Doesnt' work
    @settings.beginGroup("Database");
    QString regCode_ = settings.value("RegistrationCode", "").toString();
    qDebug() << regCode_ ;
    settings.endGroup();@

    Finally, is there a simple way to test for the existence of a particular setting (beyond doing a settings.value, with default value "" and testing for that? The ini file I am parsing may or not have any combination of the following keyvalues.

    [Key]
    KeyValue1=7
    KeyValue2=10
    KeyValue3=22
    ...
    KeyValueX



  • @ QSettings settings(cfgPath, QSettings::IniFormat);
    settings.beginGroup("Key");
    QString PlayerRanges_ = settings.value("KeyValue", "").toString();
    qDebug() << KeyValue_ ;
    @

    In this,what is "KeyValue_" ?



  • @ QSettings settings(cfgPath, QSettings::IniFormat);
    settings.beginGroup("Key");
    QString PlayerRanges_ = settings.value("KeyValue", "").toString();
    qDebug() << KeyValue_ ;
    @

    In this,what is "KeyValue_" , that you are trying to debug in line 4 ?



  • Updated code (was a typo when simplifying my code to post)....still have the same problem described in OP i.e that keyValue when in the more complex form isn't captured by QSettings.value.



  • Hi,

    @QString PlayerRanges_ = settings.value("KeyValue", "").toString();
    qDebug() << KeyValue_ ;@

    you print out something else ... the conversion to QString just works if string is with quote

    @[Key]
    KeyValue="7,10|4,6|3,3|2,2|"@

    bq. The parser returns values from this outside of a beginGroup, but not if I do a setting.beginGroup(“General”) ?

    is silly but I ask: you sure update and read the right .ini file? :) ... beginGroup should just work nicely :)



  • I don't have control of the ini file / not my ini to go changing (it is a 3rd party app)...so I can't put 7,10|4,6|3,3|2,2| in quotes.

    bq. is silly but I ask: you sure update and read the right .ini file? :) … beginGroup should just work nicely :)

    Yes, as all the other info is being parsed correctly.



  • try toStringList() which split the input value by comma ... then concatenate them or what you'd need

    @QStringList KeyValue = settings.value("KeyValue", "").toStringList();@



    1. This is why you should just cut & paste the working code instead of trying to write code on the fly in the comment box.

    2. settings.value() is returning a QStringList. You can probably use join() to get the original value.

    I found this out by writing this code:
    QVariant x = settings.value("KeyValue", "");
    and inspecting x in a debugger.


Log in to reply