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); settings.beginGroup("warnings"); //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. settings.endGroup(); 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 :)
-
@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? -
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); settings.beginGroup("warnings"); QString param = settings.value(key).toString(); settings.endGroup();
-
@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
Wordings.ini
[warnings]
SN1 = hello
SN2 = hi
SN3 = so on and so fourth.That is why I seek help :)
-
@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".
-
@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); settings.beginGroup("warnings"); QString param = settings.value(key).toString(); qDebug() << param; settings.endGroup();
And it works.
-
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 thestd::string
contains non-ASCII characters. E.g.fromStdString()
assumes the data in UTF-8.