Skip to content

Instantly share code, notes, and snippets.

@vicatcu
Created September 9, 2016 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicatcu/7668fe8f26804e911b1b7fc6ce850a99 to your computer and use it in GitHub Desktop.
Save vicatcu/7668fe8f26804e911b1b7fc6ce850a99 to your computer and use it in GitHub Desktop.
template <class Key, class T>
QString mapToString(QMap<Key, T> &map){
// this is derivative of the code that qDebug uses to serialize a QVariantMap
// qtbase/src/corelib/io/qdebug.h
// template <class Key, class T>
// inline QDebug operator<<(QDebug debug, const QMap<Key, T> &map)
QByteArray buf;
QDataStream s(&buf, QIODevice::ReadWrite);
s << "(";
for (typename QMap<Key, T>::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {
s << '(' << it.key() << ", " << it.value() << ')';
}
s << ')';
return QString::fromLatin1(buf);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QVariantMap map;
map.insert("a", QVariant("123"));
map.insert("b", QVariant("234"));
qDebug() << mapToString(map);
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment