Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Serialising variants - QVector<int>() from value.



  • Hey

    And I'm stuck, I have a

    QMap<int,QMap<int,QVariant>> as my data structure in my treeView.
    

    I was trying to serialize it but I keep getting >

    WARNING: QVariant::save: unable to save type 'QVector<int>' (type id: 1033).
    
     (kernel\qvariant.cpp:2593, void __cdecl QVariant::save(class QDataStream &) const)
    

    As far as I can tell, I have 7 items in my map and some of them are :

    QVector<int> data;
    QVariant::fromValue(data);
    

    Other than that I don't see other issues, when I look in debug at the map some of variatns have no value at all and others have (invalid) as flag next to them...

    Mhmhmmhmmm....

    What did I do ? :- )


    Kay so I did some digging, looks like I have to register it and somehow implement...but how?

        qRegisterMetaType<QVector<int>>("QVector<int>");
    
        QMap<int, QVariant> wah;
        wah[0] = QVariant::fromValue(QVector<int>{12, 231, 45, 125, 123, 12, 312, 4, 12});
        qDebug() << wah;
    
        QByteArray ar;
        QDataStream s(&ar, QIODevice::WriteOnly);
        s << wah;
    

    returns the same error...


    Tried adding this above main... still no luck...

    QDataStream &operator<<(QDataStream &out, const QVector<int> &vec) {
        out << vec.size();
        for (auto &v:vec) {
            out << v;
        }
        return out;
    };
    
    
    int main(int argc, char *argv[]) {
    

    Answer :

    Needed to add :

    qRegisterMetaTypeStreamOperators<QVector<int>>("QVector<int>");
    

    I really wish that Docs would explain that... "easier" or mention it somehow. Took a while to read a whole bunch of info to realize this :/