Qt QList <QString> serialization for database

I have a QList. I want to insert it into the database. After some googling, I couldn't find any serialization method. If there is any method / idea for serializing list data to database?


source to share

2 answers

How about using QStringList

instead QList<QString>


QStringList numberList_;    // instead of QList<QString>, use this
QString myString1 = "Hello";
QString myString2 = "World";
numberList_ << myString1;
numberList_ << myString2;

QByteArray byteArray;
QBuffer buffer(&byteArray);
QDataStream out(&buffer);
out << numberList_;


Probably QList<QString>

should also work instead QStringList

. If it isn't, you can easily convert it to QStringList


QDataStream , QBuffer , QByteArray and QStringList reference.



Here's another option that's a little more concise:

QString serialize(QStringList stringList)
  QByteArray byteArray;
  QDataStream out(&byteArray, QIODevice::WriteOnly);
  out << stringList;
  return QString(byteArray.toBase64());

QStringList deserialize(QString serializedStringList)
  QStringList result;
  QByteArray byteArray = QByteArray::fromBase64(serializedStringList.toUtf8());
  QDataStream in(&byteArray, QIODevice::ReadOnly);
  in >> result;
  return result;




All Articles