title: Serializing QVariant through QDataStream
body: <p>I'm possibly writing it wrong but here's the code that i'm trying to execute and it fails
  to do what's expected:</p>
  
  <pre><code>#include &lt;QDataStream&gt;
  #include &lt;QByteArray&gt;
  #include &lt;QVariant&gt;
  #include &lt;iostream&gt;
  
  
  int main(){
  
      QByteArray data;
      QDataStream ds(&amp;data,QIODevice::WriteOnly);
      QVariant v(123);
      ds &lt;&lt; v;
      std::cout &lt;&lt;"test: " &lt;&lt;  data.data() &lt;&lt; std::endl; // "test: 123"
  
  
      return 0;
  }
  </code></pre>
  
  <p>Given the example in the documentation of the QVariant class :</p>
  
  <p><a href="http://qt-project.org/doc/qt-5.1/qtcore/qvariant.html#type" rel="noreferrer">http://qt-project.org/doc/qt-5.1/qtcore/qvariant.html#type</a></p>
  
  <p>It should serialize the value 123 correctly to the QByteArray but doesn't do so,instead it just writes out: </p>
  
  <pre><code>test: 
  </code></pre>
  
  <p>Anybody has an idea how to fix this ?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Well, maybe it was not clear but here is the original problem:</p>
  
  <p>I have possibly any QVariant built-in type stored in the QVariant such as QStringList, QString , double, int , etc....</p>
  
  <p>What I want is a way to serialize the QVariant to a string and restoring it without having to do it myself for each type.
  As far as I know the QVariant::toString() method does not work with all types that are accepted through QVariant, and I was thinking that passing by a QDataStream could pass me a serialized version of the QVariant.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Thanks to the answer of piotruś I was able to answer my problem.
  Here's the program:</p>
  
  <pre><code>int main(){
      QString str;
      {
          //serialization
          QByteArray data;
          QDataStream ds(&amp;data,QIODevice::WriteOnly);
          QVariant v(123);
          ds &lt;&lt; v;
          data = data.toBase64();
          str = QString(data);
          cout &lt;&lt; str.toStdString() &lt;&lt; endl;
  
      }
      {
          //deserialization
          QByteArray data = str.toLatin1();
          data = QByteArray::fromBase64(data);
          QDataStream ds(&amp;data,QIODevice::ReadOnly);
          QVariant v;
          ds &gt;&gt; v;
          cout &lt;&lt; v.toInt() &lt;&lt; endl;
      }
  
      return 0;
  }
  </code></pre>