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. [Solved] Storing container with enums in QSettings

[Solved] Storing container with enums in QSettings

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.7k 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #1

    Hi,

    I have the following structure:
    @
    enum Eye {
    Invalid,
    OS,
    OD
    };
    Q_DECLARE_METATYPE(Eye)

    enum Field {
    Invalid,
    Nasal,
    Central
    };
    Q_DECLARE_METATYPE(Field)

    typedef QPair<Eye, Field> EyeField;
    Q_DECLARE_METATYPE(EyeField)
    typedef QVector<EyeField> EyeFieldList;
    Q_DECLARE_METATYPE(EyeFieldList)

    inline QDataStream& operator>>(QDataStream& in, Eye& e)
    {
    in >> (quint32&)e;
    return in;
    }

    inline QDataStream& operator<<(QDataStream& out, Eye& e)
    {
    out << (quint32&)e;
    return out;
    }

    inline QDataStream& operator>>(QDataStream& in, Field& e)
    {
    in >> (quint32&)e;
    return in;
    }

    inline QDataStream& operator<<(QDataStream& out, Field& e)
    {
    out << (quint32&)e;
    return out;
    }

    //in C++ file
    //ran at startup
    qRegisterMetaType<Eye>("Eye");
    qRegisterMetaType<Field>("Field");
    qRegisterMetaTypeStreamOperators<Eye>("Eye");
    qRegisterMetaTypeStreamOperators<Field>("Field");

    // ...

    QVariantHash settings; //contains, among other things, an EyeFieldList

    //...

    QSettings registry;
    registry.setValue("settings", settings); // BOOM!
    @

    I can't find how am supposed to make this work. I think I have defined all the needed Q_DECLARE_METATYPE, used qDeclareMetatype on all types used, and even implemented QDataStream operators for the enums (had to be inline, otherwise I'd get compilation errors on multiple definitions). Nothings helps. It compiles fine, but I still get crashes. Any tips on how to make this actually work?

    Edit:
    I have added the meta-type stuff and QDataStream implementations to the code.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Neutron Stein
      wrote on last edited by
      #2

      isn' t missing ";" after the definition of the enum ?

      Never Seen !

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        [quote author="Neutron Stein" date="1343732053"]isn' t missing ";" after the definition of the enum ?[/quote]
        It is (in the sample above, not in my actual code), but that is not my issue. Thanks for noticing though.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Can you insert a QVariantHash in QSettings? Or did you mean to iterate over the QVariantHash and insert the keys and values one by one ?
          So instead of this:
          @
          QVariantHash settings; //contains, among other things, an EyeFieldList
          QSettings registry;
          registry.setValue("settings", settings); // BOOM!
          @

          Something like this:
          @
          QVariantHash settings; //contains, among other things, an EyeFieldList
          QSettings registry;
          QVariantHash::const_iterator it = settings.constBegin();
          while(it != settings.constEnd())
          {
          registry.setValue(it.key(), it.value());
          it++;
          }
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Nope, I wanted to stick the QVariantHash itself in QSettings. Let Qt handle the serialization instead of me :-)

            However, I just managed to get it to work. It seems that you need to use Q_DECLARE_METATYPE and qRegisterMetaType for all containers involved. So, in my case, that involved:

            @
            qRegisterMetaType<Eye>("Eye");
            qRegisterMetaType<Field>("Field");
            qRegisterMetaType<EyeField>("EyeField");
            qRegisterMetaType<EyeFieldList>("EyeFieldList");
            qRegisterMetaTypeStreamOperators<Eye>("Eye");
            qRegisterMetaTypeStreamOperators<Field>("Field");
            qRegisterMetaTypeStreamOperators<EyeField>("EyeField");
            qRegisterMetaTypeStreamOperators<EyeFieldList>("EyeFieldList");
            @

            And actually a few more, that I left out of this example for the sake of keeping the overview.

            1 Reply Last reply
            0

            • Login

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