[Solved] How to store QList<QSharedPointer<Phase>> in QSettings



  • Object only has 2 int properties.
    One way I tried is convert QList<> to QVariant, but got this error
    QVariant::save: unable to save type 'QList<QSharedPointer<Phase>>' (type id: 1060).
    I do have Registered the class and implemented QDataStream

    Q_DECLARE_METATYPE( QSharedPointer<Phase>)
    QDataStream & operator << (QDataStream &out, const QSharedPointer<Phase> &phase);
    QDataStream & operator >> (QDataStream &in, QSharedPointer<Phase> &phase);
    
    setting.setValue("Phase", QVariant::fromValue(QSharedPointer<Phase>(phase));
    

    Second way should work, convert Phase properties to QStringList, and save this QStringList, later decode it back.

    Any one know why first way doesn't work?

    Thanks


  • Moderators

    You also need to register the type and its stream operators, as described in the docs:

    qRegisterMetaType<QSharedPointer<Phase>>("Phase");
    qRegisterMetaTypeStreamOperators<QSharedPointer<Phase>>("Phase");
    


  • @Chris-Kawa Thanks, That works!


Log in to reply
 

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