Saving QList<QTime> with QSettings



  • Hi all.

    I've been using custom streams operators (<< and >>) for long time to save lists and structures with success.

    But now, I've tried to save a simple list (QTime and bool list) and program crashes:
    @QVariant::save: unable to save type 'QList<QTime>' (type id: 1096).

    ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 1867@

    In QList docs says that there are both stream operators and says that it works fine if data (QTime, in this case) implements >> and <<, and it seems so.

    Someone knows where am I wrong?

    Thaaanks!!


  • Lifetime Qt Champion

    Hi,

    Did you try to qRegisterMetaTypeStreamOperators ?



  • Yes I did:
    @ qRegisterMetaType<QTime>("QTime");
    qRegisterMetaTypeStreamOperators<QTime>("QTime");
    @

    I will write a simpler project just to test it... maybe problem comes form other place.



  • In the simpler project with this code works:
    @ qRegisterMetaType<QTime>("QTime");
    qRegisterMetaTypeStreamOperators<QTime>("QTime");
    qRegisterMetaType< QList<QTime> >("QList<QTime>");
    qRegisterMetaTypeStreamOperators< QList<QTime> >("QList<QTime>");

    QSettings sett("QtTestList", "QtTestList");
    QList<QTime> listaHoras;
    listaHoras << QTime(1,2,3);
    listaHoras << QTime(4,5,6);
    sett.setValue("test", QVariant::fromValue<QList<QTime> >(listaHoras) );
    sett.sync();
    listaHoras.clear();
    listaHoras = sett.value("test").value<QList<QTime> >();
    listaHoras << QTime();@

    I will search what's wrong in the original code. I guess qRegisterMetaTypeStreamOperators are not called before QSettins::setValue(...)

    Thanks!!!


  • Lifetime Qt Champion

    It depends on when/where you call the registration functions. Ensure it's done before using QSettings.


Log in to reply
 

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