[Solved]qRegisterMetaTypeStreamOperators issue



  • 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!


  • Lifetime Qt Champion

    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



  • 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!



  • 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.


  • Lifetime Qt Champion

    Great !

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.