[Solved] QVariant and custom types

  • I’ve got a class (pubic default and copy constructors and destructor are implemented) named, say, SomeClass and SomeClassList that is a typedef for QList<SomeClass>. Stream operators are implemented for SomeClass and both SomeClass and SomeClassList are registered using Q_DECLARE_METATYPE and qRegisterMetaTypeStreamOperators.

    QSettings instance is used to store SomeClassList instances. I faced the following issue:
    SomeClassList defaultList;

    // ....

    // assuming that defaultList contains N SomeClass instances:
    QVariant variantList = QVariant::fromValue(defaultList);
    // here list will contain N SomeClass instances, everything’s fine:
    SomeClassList list = variantList.value<SomeClassList>();

    // But, if there’s nothing to read from "somelist", anotherList gets empty,
    // nothing’s copied from variantList
    SomeClassList anotherList = settings->value("somelist", variantList).value<SomeClassList>();

    What can be the reason for such a behaviour?

  • Very odd. Here are some hints as to what you can investigate:

    Try QSettings::contains() to see whether or nor QSettings thinks the value is set

    Try comparing the return value from settings->value() with you original list (i.e. don't use the conversion back to you original class, yet.

  • Actually value "somelist" was set to nothing in settings, that’s why default value wasn’t loaded. So the problem is where value is saved, not read; steps to reproduce the problem were simply incorrect.

    So, nomore questions from my side, thanks.

  • Ok, that's what I suspected.

Log in to reply

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