Important: Please read the Qt Code of Conduct -

Reading .ini files: problems with argument

  • Hi people,

    For today, I am exploring how to read on .ini files . Since I recently start to learn how to read Qt documentation, I followed closely to the syntax/argument required. The problem lies on the AREA of ATTENTION 1 where I start to use const char * at the argument "c". AREA OF ATTENTION 2 is the snippet I managed to find online and it works (Managed to extract the data I want). The reason why I dont wish to use "SN1" is because I would like "c" to act as the argument to AREA OF ATTENTION 1 line. (for flexibility)

    std::string search_num = "SN1";
    const char * c =  search_num.c_str();                              //conversion from std::string to const char *
    QSettings settings("C:/Users/Common/Desktop/PopupText/PopupText/textOption.ini", QSettings::IniFormat);
     //AREA OF ATTENTION 1 ->  QString param = settings.value(c.c_str()).toString();             
     //AREA OF ATTENTION 2  ->//QString param = settings.value("SN1").toString();                                 //snippet from online where "SN1" suggest const char*. Using this method works but not ideal due to manual keying.
        qDebug() << param;  //result in:  " "
        qDebug() << c;       // result in : SN1

    Why does this happen, and how do I resolve AREA OF ATTENTION 1 since it doesn't seems to read even though I have input the right argument type?

    Thank you and looking forward for your help :)

  • Lifetime Qt Champion

    @Faruq Sorry I don't understand your description!
    Why do you want to use char* in QSettings::value? There is exactly 0 reason to do so as it accepts QString.
    What is this:

    //AREA OF ATTENTION 1 ->  QString param = settings.value(c.c_str()).toString();             
     //AREA OF ATTENTION 2  ->//QString param = settings.value("SN1").toString();

    What is "AREA OF ATTENTION"?
    Can you show the content of your INI file and explain what you want to read from it?

  • Lifetime Qt Champion

    Hi @Faruq,

    So am I right you want something like this?

    QString key = "SN1";
    QSettings settings("C:/Users/Common/Desktop/PopupText/PopupText/textOption.ini", QSettings::IniFormat);
    QString param = settings.value(key).toString();             

  • @jsulm hi, thank you for your time. I did try Qstring but it give me a blank output(""). Why char? Because I am following closely with an example snippet. I also tried removing .tostring() but qDebug() shows that it is QVariant. Only with char constant (strictly typed with " - words-" then will it appear)

    As to what is in .ini file, it's nothing much. It's just a tag

    SN1 = hello
    SN2 = hi
    SN3 = so on and so fourth.

    That is why I seek help :)

  • Lifetime Qt Champion

    @Faruq In the code you posted you're using std::string not QString.
    Did you try what @aha_1980 posted?

  • @aha_1980 hi. Thank you for your reply. Yes. I did that but it doesn't output anything from .ini file. It only show the output if I replace "key" with ("SN1").

  • @jsulm hmm, I tried converting from std::string into Qstring too. But it doesn't work. Is it something to do with a conversion bug?

    For Qstring conversion, I used Qstring example = std_str.tostring() .

  • Lifetime Qt Champion

    @Faruq said in Reading .ini files: problems with argument:

    hmm, I tried converting from std::string into Qstring too

    Why do you convert std::string to QString at all?! Why not simply do it like @aha_1980 suggested? There is no need for std::string...
    "conversion bug" - what conversion bug?
    Also did you make sure that the path to the file is valid?

  • @jsulm I am using such conversion as the value being passed into the function is a std::string type. Thus I really require it to be as such.

    Yes. The directory is correct because if its not, I would not receive any data at all with the hard code "SN1".

  • Lifetime Qt Champion

    @Faruq The "conversion bug" is in your code.
    I just tried this:

    std::string tmp = "SN1";
    QString key = tmp.c_str();
    QSettings settings("C:/Users/q295085/Desktop/some.ini", QSettings::IniFormat);
    QString param = settings.value(key).toString();
    qDebug() << param;

    And it works.

  • Lifetime Qt Champion

    There is also QString::fromStdString, which avoids the c_str().

    Please note: std::string is, in contrast to QString, no unicode string, so you have to convert your data carefully if the std::string contains non-ASCII characters. E.g. fromStdString() assumes the data in UTF-8.

  • @jsulm alright! I have yet to check and replace the constant char into Qstring. Will test it out soon enough and will ask qn if I'm still in doubt. Thank you jsulm :)

  • @aha_1980 ah I have yet to try that. I will take note, avoid such conversion method and let you know if it works :) thank you for your help.

Log in to reply