Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Serialization and deserialization of a list of custom structures (QSettings?)
QtWS25 Last Chance

Serialization and deserialization of a list of custom structures (QSettings?)

Scheduled Pinned Locked Moved Solved General and Desktop
qsettingsqmetatypeserialization
3 Posts 2 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tmladekT Offline
    tmladekT Offline
    tmladek
    wrote on last edited by tmladek
    #1

    Hi!

    I'm writing an application which, so far, has used QSettings to store its current configuration into INI files. (The requirement is that the configuration needs to be swappable).

    This worked reasonably well, but now I need to store a list of custom structures, and I'm not sure how to go about it. Can I use Qt's metatype system and just put a QList<MyStruct> into the QSettings? Or do I abandon QSettings and write my own XML/INI/whatever serializer/deserializer?

    Thanks in advance!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Yes you can put your custom struct in QSettings but you have to do some work for it. Essentially you have to write the QDataStream operators for MyStruct and register them with qRegisterMetaTypeStreamOperators so that QVariant can use them to do the serialization/deserialization for you. You don't need to create operators for QList<MyStruct>. Qt's smart enough to use the MyStruct stream operators with it's container classes.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • tmladekT Offline
        tmladekT Offline
        tmladek
        wrote on last edited by tmladek
        #3

        Hi, thank you for the response!

        Unfortunately, when I try to save a QList<MyStruct>, I get the following error at runtime:

        QVariant::save: unable to save type 'QList<Action>' (type id: 1032).
        
        ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 2124
        Invalid parameter passed to C runtime function.
        

        This is even though I have (after some wrestling) managed to successfully save a Action (MyStruct) using QVariant::fromValue(), having registered both Action as a metatype as well as its operators in main():

        qRegisterMetaType<Action>("Action");
        qRegisterMetaTypeStreamOperators<Action>("Action");
        

        Any idea why that might be?

        edit: A-ha! For future reference, it isn't needed to create custom operators for QList<MyStruct>, but it's still required to call qRegisterMetaTypeStreamOperators() on QList<MyStruct>. Solved!

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved