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]qRegisterMetaTypeStreamOperators issue
Forum Updated to NodeBB v4.3 + New Features

[Solved]qRegisterMetaTypeStreamOperators issue

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.9k Views 1 Watching
  • 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.
  • L Offline
    L Offline
    lfdm
    wrote on 28 Mar 2013, 07:54 last edited by
    #1

    Hello,

    I'm trying to store a structure with QSettings. I followed the following instructions ("Saving custom structures and classes to QSettings":http://www.developer.nokia.com/Community/Wiki/Saving_custom_structures_and_classes_to_QSettings).

    My structure is looking like this:
    @struct ReferenceT
    {
    uint nId;
    uint nIndex;
    QColor colorRef;
    QString sLabel;
    bool bVFlip;
    ReferenceT(): nId(0), nIndex(0), colorRef(QColor(255, 0, 0)), sLabel(QObject::tr("Reference")), bVFlip(false){};
    };

    Q_DECLARE_METATYPE(ReferenceT);
    @

    Then in the C++ file for the class I'm developing I declared the stream operators like this:
    @
    QDataStream &operator<<(QDataStream &out, const ReferenceT &obj)
    {
    out << obj.nId << obj.nIndex << obj.colorRef << obj.sLabel << obj.bVFlip;
    return out;
    }

    QDataStream &operator>>(QDataStream &in, ReferenceT &obj)
    {
    in >> obj.nId >> obj.nIndex >> obj.colorRef >> obj.sLabel >> obj.bVFlip;
    return in;
    }
    @

    I declared the type and stream operator like this:
    @
    qRegisterMetaType<ReferenceT>("ReferenceT");
    qRegisterMetaTypeStreamOperators<ReferenceT>("ReferenceT");
    @

    My save settings function looks like this:
    @
    QSetting settings;
    ReferenceT reference;
    // ...
    settings.setValue(sRef, qVariantFromValue(reference));
    @

    The save function appear to save the actual data. When I look at the binary signatures, there are different for references having different information. If I set a break point in "operator<<" function, it breaks.

    The restore settings function looks like this:
    @
    QSetting settings;
    QVariant var = settings.value("references/ref1");
    ReferenceT reference = var.value<ReferenceT>();
    @

    The reference variable does not contain any of the stored information, just the default parameters from the structure initialization. Secondly, if I set a break point to the "operator>>" function, it is never called.

    Does anybody spot any flaws in the implementation?

    Thanks and cheers!

    Damien LEFEVRE - http://www.lfdm.net

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Mar 2013, 08:18 last edited by
      #2

      Hi,

      Did you declare the stream operators in the header of you structure ?

      Also check for a message in the console that says something like unable to save/load type XXX (I can't remember exactly the text of the error). This tells you that the metatype system doesn't know the stream operators of your class/structure.

      Last thing I can think of: are you sure that your using the same key to save and load from QSettings ? Once you have the sRef variable and next you have the "references/ref1", they might not be the same.

      Hope it helps

      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
      0
      • L Offline
        L Offline
        lfdm
        wrote on 28 Mar 2013, 08:46 last edited by
        #3

        HI SGaist!

        I tried to declare the operator in the header as you suggested, but it didn't help. The problem is that the out stream function is called but not the input stream.

        The keywords are identical and actually listed as I go them through one by one.

        The console has no warnings. The message handler set to the application to build the run log has no issues listed there other than my own messages.

        I'll keep digging =)

        Thanks!

        Damien LEFEVRE - http://www.lfdm.net

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lfdm
          wrote on 28 Mar 2013, 11:00 last edited by
          #4

          Ah I found the problem, which was a logic problem.

          Wrong code
          @
          QSettings settings;
          settings.beginGroup("references/");
          QStringList keys = settings.childKeys();
          int nKeys = keys.size();

          settings.endGroup();

          for (int n = 0; n < nSize; n++)
          {
          if (n < nKeys)
          {
          QVariant var = settings.value(keys.at(n));

          if (var.isValid())
          {
          ReferenceT reference = var.value<ReferenceT>();
          // ...
          }
          }
          }
          @

          Correct code
          @
          QSettings settings;
          settings.beginGroup("references/");
          QStringList keys = settings.childKeys();
          int nKeys = keys.size();

          for (int n = 0; n < nSize; n++)
          {
          if (n < nKeys)
          {
          QVariant var = settings.value(keys.at(n));

          if (var.isValid())
          {
          ReferenceT reference = var.value<ReferenceT>();
          // ...
          }
          }
          }

          settings.endGroup();
          @

          The paths returned from childKeys were relative and not absolute.

          Damien LEFEVRE - http://www.lfdm.net

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 28 Mar 2013, 11:21 last edited by
            #5

            Great !

            Thanks for sharing, don't forget to update the thread's title to solved :)

            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
            0

            3/5

            28 Mar 2013, 08:46

            • Login

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