Regarding Using QSettings to read INI File
-
Hi All
I am trying to read INI File using QSettings. Below is my format of INI File & code to read that file
INI File Format:
[TAG1]
key1=value1
key2=value2
.
.
.
.
.
.
.CODE:
@QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("TAG1");
const QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
QString var = settings.value(childKey).toString();
settings.endGroup();@My problem here is I want to retrieve all values & store in some array or List How to do it.
But as I am retieving values in QString I am able to get only last value. -
Try using "allKeys":http://doc.qt.nokia.com/stable/qsettings.html#allKeys
@
QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("TAG1");
QStringList keys = settings.allKeys();
@ -
Hi Leon
Thanks for reply,but wat I am looking for is we retrieved all keys
in QStringList,Likewise I also want all the values of these keys
inQStringList.So that i can use those values.Regadrs
Indrajeet -
You mean like:
@
QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("TAG1");
const QStringList childKeys = settings.childKeys();
QStringList values;
foreach (const QString &childKey, childKeys)
values << settings.value(childKey).toString();
settings.endGroup();
@ -
Hi loladiro
Ya thanks this is wat i required.
One more thing my INI file has only one key & one value. But while debugging
cursor enters foreach loop thrice why is it so.Below is my INI file contents
[Shared Folder]
commonpath=c:\desktop\QT\QTDOC1Regards
Indrajeet -
The foreach loop is really just a macro used by Qt. And it expands to this (at least on gcc, it's different for other compilers):
@
for (QForeachContainer<typeof(container)> container(container);
!container.brk && container.i != container.e;
extension ({ ++container.brk; ++container.i; }))
for (variable = *container.i;; extension ({--container.brk; break;}))
@
And this causes the debugger to go through all the steps in the expanded macro, while showing you just the macro (if that makes sense). -
Perhaps, instead of storing your values in two QStringLists, you would like to store them in a single QHash<QString, QString> ?
That way, at least you keep your keys and values together in the same container...
To get them, you could modify the example code from above to something like this:
@
QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("TAG1");
const QStringList childKeys = settings.childKeys();
QHash<QString, QString> values;
foreach (const QString &childKey, childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
@ -
bq. How to handle if INI file is not found.
You could handle it directly in the settings since the settings is opened in a read-write mode.
From Documentation:
bq. Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent. If the file doesn't already exist, it is created.
Now, the value() function takes a default value.
@ QVariant value ( const QString & key, const QVariant & defaultValue = QVariant() ) const @So, if no setting, then your default value passed is used. This is the way to do it.
Here's another approach just for kicks..
From QSetting documentation, on windows, the user path seems to be
@ %APPDATA% @
and system path
@ %COMMON_APPDATA% @
Not on a windows machine right now, but you could try something like
@
QString iniDir = QProcessEnvironment::systemEnvironment ().value("COMMON_APPDATA")
bool iniExists = QFileInfo(iniDir, "test.ini").exists();
@
Then if no settings file, instead of reading the settings.. use default settings, when closing, store the settings into the ini. -
"QSettings Constructor":http://doc.qt.nokia.com/stable/qsettings.html#QSettings-4
You don't need to do anything to handle missing file. If the ini filename you pass isn't present Qt will create it. So..
@ QSettings settings("test.ini", QSettings::IniFormat); @
Will automatically create the test.ini file, if it doesn't exist.
Now, in your code to read from settings.
@ foreach (const QString &childKey, childKeys)
values.insert(childKey, settings.value(childKey).toString()); @
You can pass a default argument like
@ values.insert(childKey, settings.value(childKey, "defaultval").toString()); @
So that should do for reading the settings.When you write the settings back to the ini later on, you will be saving the default values you have read above since there was no ini to read from. I hope thats clear.
-
Hello jim
Below is my code snippet
@QSettings settings("test.ini", QSettings::IniFormat);
settings.beginGroup("TAG1");
const QStringList childKeys = settings.childKeys();
foreach (const QString &childKey, childKeys)
QString var = settings.value(childKey).toString();
settings.endGroup();@You said that if test.ini is not found it will create the new by itself.
But wat about the contents of the file.As I am looking for TAG1 it is
crashing there.So wat is the solution for this.
Regards
Indrajeet -
I don't see why it would crash! The beginGroup simply sets the prefix to use for the keys. If your key doesn't exist
@ settings.value(childKey).toString() @
will return a empty QVariant(). You can also provide a default value like I have said above.For the next question..
Taking code from others who have answered it above.
@
QSettings settings("test.ini", QSettings::IniFormat);
const QStringList childKeys = settings.allKeys();
QStringList values;
foreach (const QString &childKey, childKeys)
values << settings.value(childKey).toString();
settings.endGroup();
@I think, you have all the info you need. Try to experiment and debug to see what is going on if there is a problem. And post the offending code.
-
@jim_kaiser
HiThis is my code here
And i got some error.
Is there anything i need to fix?This is an error here
Please help!