Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>QDataStream handles a variety of C++ and Qt data types. The complete list is available at <a href="http://doc.qt.io/qt-4.8/datastreamformat.html" rel="noreferrer">http://doc.qt.io/qt-4.8/datastreamformat.html</a>. We can also add support for our own custom types by overloading the &lt;&lt; and >> operators. Here's the definition of a custom data type that can be used with QDataStream:</p> <pre><code>class Painting { public: Painting() { myYear = 0; } Painting(const QString &amp;title, const QString &amp;artist, int year) { myTitle = title; myArtist = artist; myYear = year; } void setTitle(const QString &amp;title) { myTitle = title; } QString title() const { return myTitle; } ... private: QString myTitle; QString myArtist; int myYear; }; QDataStream &amp;operator&lt;&lt;(QDataStream &amp;out, const Painting &amp;painting); QDataStream &amp;operator&gt;&gt;(QDataStream &amp;in, Painting &amp;painting); </code></pre> <p>Here's how we would implement the &lt;&lt; operator:</p> <pre><code>QDataStream &amp;operator&lt;&lt;(QDataStream &amp;out, const Painting &amp;painting) { out &lt;&lt; painting.title() &lt;&lt; painting.artist() &lt;&lt; quint32(painting.year()); return out; } </code></pre> <p>To output a Painting, we simply output two QStrings and a quint32. At the end of the function, we return the stream. This is a common C++ idiom that allows us to use a chain of &lt;&lt; operators with an output stream. For example:</p> <p>out &lt;&lt; painting1 &lt;&lt; painting2 &lt;&lt; painting3;</p> <p>The implementation of operator>>() is similar to that of operator&lt;&lt;():</p> <pre><code>QDataStream &amp;operator&gt;&gt;(QDataStream &amp;in, Painting &amp;painting) { QString title; QString artist; quint32 year; in &gt;&gt; title &gt;&gt; artist &gt;&gt; year; painting = Painting(title, artist, year); return in; } </code></pre> <p>This is from: C++ GUI Programming with Qt 4 By Jasmin Blanchette, Mark Summerfield </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload