Assigning to nested QVariantMap
Question
#include <QtCore/QCoreApplication>
#include <QVariant>
#include <QtDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVariantMap map;
map["foo"] = QVariant(QVariantMap());
map["baz"] = "asdf";
qvariant_cast<QVariantMap>(map["foo"])["bar"] = "a";
qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString();
qDebug() << map["baz"].toString();
return a.exec();
}
I am trying to assign to a QVariant within a nested QVariantMap. The first qDebug() outputs nothing, but the second outputs "asdf" as expected. How would I assign the "bar" key in the nested variable map to a value?
Solution
The issue is that qvariant_cast doesn't return a reference to the internals of the QVariant that it is operating on; it returns a copy. As such, if you overwrite the "foo" element in your top-level map with a new child map, the code will work properly:
#include <QtCore/QCoreApplication>
#include <QVariant>
#include <QtDebug>
int main(int argc, char** argv)
{
QCoreApplication a(argc, argv);
QVariantMap map;
map["foo"] = QVariant(QVariantMap());
map["baz"] = "asdf";
QVariantMap newMap;
newMap["bar"] = "a";
map["foo"] = QVariant(newMap);
qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString();
qDebug() << map["baz"].toString();
return a.exec();
}
Presumably, you want to modify the existing map instead of overwritting it. You can accomplish this by copying the existing map, adding the new data (which will result in a deep copy), and then writing the map back in:
QVariantMap existingMap = qvariant_cast<QVariantMap>(map["foo"]);
existingMap["bar"] = "a";
map["foo"] = QVariant(existingMap);
If you're considering storing a large amount of data, you may wish to reconsider your use of QVariant.
OTHER TIPS
Or you could do it the way the trollies don't like.
#include <QtCore/QCoreApplication>
#include <QVariant>
#include <QtDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVariantMap map;
map["foo"] = QVariant(QVariantMap());
map["baz"] = "asdf";
static_cast<QVariantMap>(map["foo"].data_ptr())["bar"] = "a";
qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString();
qDebug() << map["baz"].toString();
return a.exec();
}
Or you could do it all safe and nice and use a QExplicitlySharedDataPointer instead of directly a QVariantMap. Like this:
#include <QtCore>
#include <QtDebug>
class VarMap : public QVariantMap, public QSharedData {};
typedef QExplicitlySharedDataPointer<VarMap> SharedVarMap;
Q_DECLARE_METATYPE(SharedVarMap)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVariantMap map;
map["foo"] = SharedVarMap(new VarMap());
map["baz"] = "asdf";
map["foo"].value<SharedVarMap>()->["bar"] = "a";
qDebug() << map["foo"].value<SharedVarMap>()->["bar"].toString();
qDebug() << map["baz"].toString();
return a.exec();
}
When qvariant_cast is executed, object is copied. you should use a pointer of the object. You can try the following code instead of qvariant_cast code.
QVariantMap* m = (QVariantMap*)(map["foo"].data());
(*m)["bar"] = "a";
template <typename T>
inline T& getStoredValueRef(QVariant &v)
{
const auto type = qMetaTypeId<T>(static_cast<T*>(nullptr));
auto& d = v.data_ptr();
if (type == d.type)
{
auto data = reinterpret_cast<T*>(d.is_shared ? d.data.shared->ptr : &d.data.ptr);
return *data;
}
throw std::runtime_error("Bad type");
}
use it as
(getStoredValueRef<QVariantMap>(map["foo"]))["bar"] = "a";
and more deep
(getStoredValueRef<QVariantMap>(
(getStoredValueRef<QVariantMap>(map["foo"]))["bar"]))["zoo"] = "a";
Starting from Qt 5.1, you can use the C++11 uniform initialization syntax to build a nested QMap
or QVariantMap
easily:
QVariantMap fooMap{
{"bar", "a"}
};
QVariantMap map{
{"foo", fooMap}, // nested map
{"baz", "asdf"}
};
qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString(); // outputs "a"
qDebug() << map["baz"].toString(); // outputs "asdf"