PDA

View Full Version : how to read QMap from QSettings?



nuliknol
4th May 2014, 01:40
Hi,
I have problems reading a QMap value from QSettings. I write the data and it is written, but when I read it I get nothing.

I create settings file like this:



settings=new QSettings("identities.conf",QSettings::IniFormat);
QMap<QString, QVariant> imap;
imap.insert("user1@gmail.com","John Smith");
imap.insert("user2@gmail,com","Juan Perez");
settings->setValue("identitites",imap);

Then I try to read it and I get an empty list, here is how I do it:


int size;
QList <QString> ql;
QVariant qv;
QMap<QString, QVariant> *imap_ptr;
qv=settings->value("identities");
qWarning() << "qv" << qv;
imap_ptr=new QMap<QString,QVariant>(qv.toMap());
qWarning() << "map:" << imap_ptr;
qWarning() << "keys" << imap_ptr->keys();
ql=imap_ptr->keys();
size=ql.size();
qWarning() << "list size=" << size;

The debug output is this:

qv QVariant(Invalid)
map: 0x22f1eb0
keys ()
list size= 0

the settings file is not empty , I can see the data:

[niko@localhost build-forms-Desktop_Qt_5_2_1_GCC_64bit-Debug]$ cat identities.conf
[General]
identitites="@Variant(\0\0\0\b\0\0\0\x2\0\0\0\x1e\0u\0s\0\x65\0 r\0\x32\0@\0g\0m\0\x61\0i\0l\0,\0\x63\0o\0m\0\0\0\ n\0\0\0\x14\0J\0u\0\x61\0n\0 \0P\0\x65\0r\0\x65\0z\0\0\0\x1e\0u\0s\0\x65\0r\0\x 31\0@\0g\0m\0\x61\0i\0l\0.\0\x63\0o\0m\0\0\0\n\0\0 \0\x14\0J\0o\0h\0n\0 \0S\0m\0i\0t\0h)"
[niko@localhost build-forms-Desktop_Qt_5_2_1_GCC_64bit-Debug]$


Probably, I am not using methods correcly, how would I read a QMap stored in settings file ?

anda_skoa
4th May 2014, 14:37
You are using different key for saving and for loading.

"identitites" vs. "identities"

Cheers,
_

nuliknol
4th May 2014, 15:59
good eye!
thanks a lot, works perfectly now

AlekseyK
12th October 2018, 12:22
Why it worked in Qt 5.2 and does not work for me in Qt 5.11 - stores nothing to settings if try to save QVariantMap type?

d_stranz
12th October 2018, 18:02
stores nothing to settings if try to save QVariantMap type?

If you showed some code it would help to get you an answer.