Results 1 to 5 of 5

Thread: how to read QMap from QSettings?

  1. #1

    Default how to read QMap from QSettings?

    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:

    Qt Code:
    1. settings=new QSettings("identities.conf",QSettings::IniFormat);
    2. QMap<QString, QVariant> imap;
    3. imap.insert("user1@gmail.com","John Smith");
    4. imap.insert("user2@gmail,com","Juan Perez");
    5. settings->setValue("identitites",imap);
    To copy to clipboard, switch view to plain text mode 

    Then I try to read it and I get an empty list, here is how I do it:
    Qt Code:
    1. int size;
    2. QList <QString> ql;
    3. QMap<QString, QVariant> *imap_ptr;
    4. qv=settings->value("identities");
    5. qWarning() << "qv" << qv;
    6. imap_ptr=new QMap<QString,QVariant>(qv.toMap());
    7. qWarning() << "map:" << imap_ptr;
    8. qWarning() << "keys" << imap_ptr->keys();
    9. ql=imap_ptr->keys();
    10. size=ql.size();
    11. qWarning() << "list size=" << size;
    To copy to clipboard, switch view to plain text mode 

    The debug output is this:
    Qt Code:
    1. qv QVariant(Invalid)
    2. map: 0x22f1eb0
    3. keys ()
    4. list size= 0
    To copy to clipboard, switch view to plain text mode 

    the settings file is not empty , I can see the data:
    Qt Code:
    1. [niko@localhost build-forms-Desktop_Qt_5_2_1_GCC_64bit-Debug]$ cat identities.conf
    2. [General]
    3. identitites="@Variant(\0\0\0\b\0\0\0\x2\0\0\0\x1e\0u\0s\0\x65\0r\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\x31\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)"
    4. [niko@localhost build-forms-Desktop_Qt_5_2_1_GCC_64bit-Debug]$
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,462 Times in 1,416 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to read QMap from QSettings?

    You are using different key for saving and for loading.

    "identitites" vs. "identities"

    Cheers,
    _

  3. #3

    Default Re: how to read QMap from QSettings?

    good eye!
    thanks a lot, works perfectly now

  4. #4
    Join Date
    Nov 2010
    Posts
    39
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to read QMap from QSettings?

    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?

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,819
    Thanks
    216
    Thanked 590 Times in 580 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: how to read QMap from QSettings?

    stores nothing to settings if try to save QVariantMap type?
    If you showed some code it would help to get you an answer.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 14th January 2011, 12:57
  2. How do I read a QMap<int, QPushButton*> ?
    By crowBar in forum Qt Programming
    Replies: 7
    Last Post: 30th December 2010, 23:39
  3. QSettings to read .plist on Mac
    By lemmiwinks in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2010, 22:13
  4. QSettings , read only avaiable?
    By patrik08 in forum Qt Programming
    Replies: 5
    Last Post: 18th November 2007, 16:21
  5. Is QMap efficient in case of frequent read access ?
    By yellowmat in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2006, 09:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.