Problem converting QVariantMap to QObject
-
Hi.
I want to convert aQVariantMap
to aQObject
with corresponding properties.
I tried both QJson and QtPropertySerializer. Both are great in converting simple data types (e.g.int
), but can not convert toQList<int>
I have a property of typeQList<int>
in myQObject
-derived class and some otherint
properties.
After callingQObjectHelper::qvariant2qobject
orQtPropertySerializer::deserialize
int properties are ok and have correct values, butQList<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
-
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>, ))
AlthoughqDebug
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)
-
@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
ClassQ_PROPERTY(QList<int> savedCosts READ savedCosts WRITE setSavedCosts)