Solved Insert data into QHash inside QHash
-
Hello this is how I declared variable of QHash:
QHash<QString, QHash<QString, QString>> data
As you can see I want to have QHash inside QHash, because I would like to have one variable which holds some sections like args, settings etc.. and some keys with values so I tried to insert some data into that variable like:
data.insert("args", "test", "123"); data.insert("args", "test2", "12345"); data.insert("settings", "key", "123456");
But I got:
error: no matching function for call to 'QHash<QString, QHash<QString, QString> >
I understand that my insert is wrong because I insert 3x QString but I have to insert 1x QString and 1x QHash which contains that others 2x QStrings, but I don't know how can I do it, I don't know syntax. So I would be happy if someone helps me.
-
Hi
One way would beusing MyHash = QHash<QString, QString> ; // to raise readability QHash<QString, MyHash> data; MyHash h1; h1.insert("key", "value"); h1.insert("key2", "value3"); data.insert("args", h1);
There might be a fancier syntax but i like this for simplicity :)
If you want a predefined list, you can also do
QHash<QString, QHash<QString, QString>> LookupList { {"A", {{ "....", "..."}}}, {"b", {{ "...", "..."}}}, {"x", {{ "...", "..."}}}, };
You can of course still insert items at runtime.
-
@mrjj If I use predefined list then how can I insert items at runtime? Like in first method:
MyHash h1; h1.insert("key", "value"); h1.insert("key2", "value3"); data.insert("args", h1);
?
-
@t0msk sure, but why don't you just try it? ;)
-
@t0msk
yes. same way.
its 100% the same as first, except there will be already values in it on run. -