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

Problem converting QVariantMap to QObject



  • Hi.
    I want to convert a QVariantMap to a QObject with corresponding properties.
    I tried both QJson and QtPropertySerializer. Both are great in converting simple data types (e.g. int), but can not convert to QList<int>
    I have a property of type QList<int> in my QObject-derived class and some other int properties.
    After calling QObjectHelper::qvariant2qobject or QtPropertySerializer::deserialize int properties are ok and have correct values, but QList<int> properties are all empty. Also there is no warnings or errors.
    What's the problem? how can I manually convert them to correct values?

    Thanks


  • Lifetime Qt Champion

    Hi
    Both QJson and QtPropertySerializer are external to Qt so unless other users here
    also used these 3rd party addon,
    its unlikely you can get an answer here and should maybe ask the authors about it.

    Also when you serialize with QtPropertySerializer to json, is the
    QList property even included in the output ?



  • @mrjj Yes it is included in the qDebug output:
    ("savedCosts", QVariant(QList<int>, ))
    Although qDebug shows it empty. but when I deserialize this data back to another object, it has actual values :/

    Bart *b = new Bart;
    b->setSavedCosts({1, 2, 3}); // <- QList<int>
    qDebug() << QtPropertySerializer::serialize(b); // <- I showed part of the output, in above code
    Bart *b2 = new Bart;
    QtPropertySerializer::deserialize(b2, QtPropertySerializer::serialize(b));
    qDebug() << b2->savedCosts(); // prints (1, 2, 3)
    

  • Lifetime Qt Champion

    @MajidKamali
    Ok, so QtPropertySerializer actually support it/knows Qlist and
    if it read it into b2 correctly then which part is not working for you?



  • @mrjj I changed the code a little bit and found something strange :|

    QObject *b = new Bart;
    b->setProperty("savedCosts", QVariant::fromValue(QList<int>{1, 2, 3}));
    qDebug() << QtPropertySerializer::serialize(b); // <- I showed part of the output, in above code
    QObject *b2 = new Bart;
    QtPropertySerializer::deserialize(b2, QtPropertySerializer::serialize(b));
    qDebug() << b2->property("savedCosts").toList(); // <- THIS LINE PRINTS EMPTY -> ()
    qDebug() << qobject_cast<Bart*>(b2)->savedCosts(); // <- BUT THIS DOES NOT -> (1, 2, 3)
    

    I thinks it's Qt-related now :D



  • This is property declaration in Bart Class

    Q_PROPERTY(QList<int> savedCosts READ savedCosts WRITE setSavedCosts)
    

Log in to reply