Regarding Using QSettings to read INI File
-
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.
-
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!
-
@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!
@victor-wang Why did you put ; after foreach?
qDebug() instead of qDebug -
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();
@@andre Hi sir, I tried this method and I am able to read 1 tag values but if I try to read 2nd tag values I am not getting. Can you please help me with this.
QSettings settings("abc.ini", QSettings::IniFormat);
settings.beginGroup("tag 1");
const QStringList childKeys = settings.childKeys();
QHash<QString,QString>values;
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
qDebug()<<values;
This was my code for 1 tag(Attributes)
but for other tag I am not getting
[tag 1]
key1=value1
key2=value2
key3=value3
key4=value4
.
.
[tag2]
key1=value1
key2=value2
key3=value3
key4=value4
.
.
Now how to read this tag2 I am unable to do. Can you pls tell me.
Thank you. -
@andre Hi sir, I tried this method and I am able to read 1 tag values but if I try to read 2nd tag values I am not getting. Can you please help me with this.
QSettings settings("abc.ini", QSettings::IniFormat);
settings.beginGroup("tag 1");
const QStringList childKeys = settings.childKeys();
QHash<QString,QString>values;
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
qDebug()<<values;
This was my code for 1 tag(Attributes)
but for other tag I am not getting
[tag 1]
key1=value1
key2=value2
key3=value3
key4=value4
.
.
[tag2]
key1=value1
key2=value2
key3=value3
key4=value4
.
.
Now how to read this tag2 I am unable to do. Can you pls tell me.
Thank you.@poojakamshetty
Hi and welcome to the forumsBut did you try with
settings.beginGroup("tag2);
and then same code otherwise ? -
@poojakamshetty
Hi and welcome to the forumsBut did you try with
settings.beginGroup("tag2);
and then same code otherwise ?@mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
QSettings settings("abc.ini", QSettings::IniFormat);
QHash<QString,QString>values;
settings.beginGroup("tag1");
QStringList childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
settings.beginGroup("tag2");
childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
qDebug()<<"Key-value pair:"<<values;
output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
order of my mini file is
[tag1]
k=v
a=c
d=m
[tag2]
k2=v2
a2=c2
d2=m2
Can you tell how to get in order -
@mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
QSettings settings("abc.ini", QSettings::IniFormat);
QHash<QString,QString>values;
settings.beginGroup("tag1");
QStringList childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
settings.beginGroup("tag2");
childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
qDebug()<<"Key-value pair:"<<values;
output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
order of my mini file is
[tag1]
k=v
a=c
d=m
[tag2]
k2=v2
a2=c2
d2=m2
Can you tell how to get in order@poojakamshetty Please check documentation: https://doc.qt.io/qt-5/qhash.html "With QHash, the items are arbitrarily ordered". With QMap data is sorted by key.
Why do you need same order in the container? -
@mrjj Hi sir, Yeah I tried settings.beginGroup("tag2"); I am getting the output but my key=values are not coming in order. How can I get in order?
QSettings settings("abc.ini", QSettings::IniFormat);
QHash<QString,QString>values;
settings.beginGroup("tag1");
QStringList childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
settings.beginGroup("tag2");
childKeys = settings.childKeys();
foreach (const QString &childKey,childKeys)
values.insert(childKey, settings.value(childKey).toString());
settings.endGroup();
qDebug()<<"Key-value pair:"<<values;
output: Key-value pair: QHash(("a2", "c2")("d", "m")("k", "v")("d2", "m2")("a", "c")("k2", "v2"))
order of my mini file is
[tag1]
k=v
a=c
d=m
[tag2]
k2=v2
a2=c2
d2=m2
Can you tell how to get in order@poojakamshetty
Hi
The out of order comes from
QHash<QString, QString>values;
Is what we call unordered
You can use QMap instead
QMap<QString, QString>values;and then it should come in the expected order.
(meh i type too slow :))
-
@poojakamshetty Please check documentation: https://doc.qt.io/qt-5/qhash.html "With QHash, the items are arbitrarily ordered". With QMap data is sorted by key.
Why do you need same order in the container?@jsulm yeah understood. Thank you. But I want in the same order in the container because I can access it easily later. But in the same order, I am not getting my output
-
@poojakamshetty
Hi
The out of order comes from
QHash<QString, QString>values;
Is what we call unordered
You can use QMap instead
QMap<QString, QString>values;and then it should come in the expected order.
(meh i type too slow :))
@mrjj I tried this but I am not getting my output as expected order.
-
@jsulm yeah understood. Thank you. But I want in the same order in the container because I can access it easily later. But in the same order, I am not getting my output
But in the same order, I am not getting my output
I wonder what that means? Have you changed
QHash
over toQMap
?QMap
is ordered, but only by key name (i.e. alphabetically). If you retrieve them from the file in a particular order, you will have to use a list/array instead if you wish to retain that particular order. -
But in the same order, I am not getting my output
I wonder what that means? Have you changed
QHash
over toQMap
?QMap
is ordered, but only by key name (i.e. alphabetically). If you retrieve them from the file in a particular order, you will have to use a list/array instead if you wish to retain that particular order.@JonB Got it,thank you. I am getting in alphabetical order when I am using QMap. My Requirement is to get in the order the same as my .ini file. What should I do?
-
@JonB Got it,thank you. I am getting in alphabetical order when I am using QMap. My Requirement is to get in the order the same as my .ini file. What should I do?
@poojakamshetty
hi
Well store them in a list instead. that will store them as added but you lose
the ability to lookup by key but maybe thats fine. -
@poojakamshetty
hi
Well store them in a list instead. that will store them as added but you lose
the ability to lookup by key but maybe thats fine.@mrjj Hi,
If I store them in a list I think I won't get the output as key-value pair right? -
@mrjj Hi,
If I store them in a list I think I won't get the output as key-value pair right?@poojakamshetty
Hi
Not if you only store a string but you could easily just do
QVector<QPair<QString, QString>> list;
or a similar structure to store both key and value in the same list. -
@poojakamshetty
Hi
Not if you only store a string but you could easily just do
QVector<QPair<QString, QString>> list;
or a similar structure to store both key and value in the same list.This post is deleted! -
@poojakamshetty
Hi
Not if you only store a string but you could easily just do
QVector<QPair<QString, QString>> list;
or a similar structure to store both key and value in the same list.@mrjj Hi
Okay, Got it Thank you.
I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later? -
@mrjj Hi
Okay, Got it Thank you.
I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later?@poojakamshetty
Hi
They will be stored in same order as you add them so if you add them in the order you read them it should be as you want. (same order as in ini file) -
@mrjj Hi
Okay, Got it Thank you.
I have a doubt. In the container, the values can be stored in any order or if they are not in the order as .ini file will that be any problem later?if they are not in the order as .ini file will that be any problem later?
The ini file/
QSettings
does not care about the order of entries. Only you seem to want this:My Requirement is to get in the order the same as my .ini file.
Why do you even need this?