Solved Reading .ini files: problems with argument
-
@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 :)
-
-
@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() .
-
@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. -
@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.