<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Sending infomartion like double and image with TCP]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I'm new with Qt and I want to transmit some information about TCP. I use a QByteArray and a QDataStream for sending:</p>
<p dir="auto">@QByteArray block;<br />
QDataStream out(&amp;block, QIODevice::WriteOnly);<br />
out.setVersion(QDataStream::Qt_5_0);<br />
out &lt;&lt; (quint16)0;<br />
out &lt;&lt; scaleFactor;  //double<br />
out &lt;&lt; imageData;  //any format to transmit my image<br />
out.device()-&gt;seek(0);<br />
out &lt;&lt; (quint16)(block.size() - sizeof(quint16));</p>
<p dir="auto">tcpSocket-&gt;write(block);@</p>
<p dir="auto">and for receiving (by "readyRead()"):</p>
<p dir="auto">@QDataStream in(clientConnection);<br />
in.setVersion(QDataStream::Qt_5_0);</p>
<p dir="auto">if (blockSize == 0) {<br />
if (clientConnection-&gt;bytesAvailable() &lt; (int)sizeof(quint16))<br />
return;<br />
in &gt;&gt; blockSize;<br />
}<br />
if (clientConnection-&gt;bytesAvailable() &lt; blockSize)<br />
return;</p>
<p dir="auto">QByteArray img;  // here should be the format in which I receive the data for my image<br />
double scaleFactor = 0.0;</p>
<p dir="auto">in &gt;&gt; scaleFactor;<br />
in &gt;&gt; imageData;</p>
<p dir="auto">qDebug() &lt;&lt; "scaleFactor:" &lt;&lt; scaleFactor; // this works, so transmitting a double is no problem<br />
img.loadFromData(imageData);@</p>
<p dir="auto">I tried to transmitt my QImage as a QByteArray:</p>
<p dir="auto">@QByteArray ba;<br />
QBuffer buffer(&amp;ba);<br />
image.save(&amp;buffer, "PNG");<br />
QByteArray imageData = buffer.data();@</p>
<p dir="auto">but I think QDataStream can't work with QByteArray. Then I tried to use:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/const">@<bdi>const</bdi></a> char *imageData = buffer.data().data();@</p>
<p dir="auto">but when I want to tranform the char back to a QByteArray which I need to create my QImage, this didn't work:</p>
<p dir="auto">@//this works:<br />
QByteArray ba;<br />
QBuffer buffer(&amp;ba);<br />
image.save(&amp;buffer, "PNG");<br />
QByteArray ba2 = buffer.data();<br />
QImage test;<br />
test.loadFromData(ba2);</p>
<p dir="auto">//and this not:<br />
QByteArray ba;<br />
QBuffer buffer(&amp;ba);<br />
image.save(&amp;buffer, "PNG");<br />
QByteArray ba2 = buffer.data();<br />
const char *imageData = ba2.data();<br />
QByteArray ba3 = QByteArray(imageData);<br />
QImage test;<br />
test.loadFromData(ba3);@</p>
<p dir="auto">So in the last version I would have my char which I could transmit by QDataStream, but I can't get my image from it.</p>
<p dir="auto">Maybe there is a much easier way to transmit a double and an QImage??<br />
Or can you show my any way how to do it? But important is, that I have to transmit both, the double an the QImage.<br />
Thank you!!!</p>
]]></description><link>https://forum.qt.io/topic/27139/sending-infomartion-like-double-and-image-with-tcp</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 11:24:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/27139.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 May 2013 11:01:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 15:10:04 GMT]]></title><description><![CDATA[<p dir="auto">That was the second idea I had in mind but I failed to find the post.</p>
<p dir="auto">If everything is working as expected, don't forget to update the thread's title to solved so other forum users may know that a solution has been found :)</p>
]]></description><link>https://forum.qt.io/post/179082</link><guid isPermaLink="true">https://forum.qt.io/post/179082</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 13 May 2013 15:10:04 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 15:03:06 GMT]]></title><description><![CDATA[<p dir="auto">My image was too big, "this helped.":<a href="http://qt-project.org/forums/viewthread/25609" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/forums/viewthread/25609</a></p>
]]></description><link>https://forum.qt.io/post/179077</link><guid isPermaLink="true">https://forum.qt.io/post/179077</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 13 May 2013 15:03:06 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 10:22:31 GMT]]></title><description><![CDATA[<p dir="auto">I used that example. Before the "in &gt;&gt; .." I have this:<br />
@QDataStream in(clientConnection);<br />
in.setVersion(QDataStream::Qt_5_0);</p>
<p dir="auto">if (blockSize == 0) {<br />
if (clientConnection-&gt;bytesAvailable() &lt; (int)sizeof(quint16))<br />
return;<br />
in &gt;&gt; blockSize;<br />
}<br />
if (clientConnection-&gt;bytesAvailable() &lt; blockSize)<br />
return;@</p>
<p dir="auto">Doesn't that mean that all data is arrived?</p>
]]></description><link>https://forum.qt.io/post/179008</link><guid isPermaLink="true">https://forum.qt.io/post/179008</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 13 May 2013 10:22:31 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 10:07:08 GMT]]></title><description><![CDATA[<p dir="auto">You are taking the wrong assumption for the receiver end. All the data won't have necessarily arrived when the readyRead signal is emitted, it only means that you have something available.</p>
<p dir="auto">Have a look at the Fortune Server and Client example. It explains one way of doing the transfer from one side to the other.</p>
<p dir="auto">Hope it helps</p>
]]></description><link>https://forum.qt.io/post/179006</link><guid isPermaLink="true">https://forum.qt.io/post/179006</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 13 May 2013 10:07:08 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 09:42:14 GMT]]></title><description><![CDATA[<p dir="auto">Can it be, that the char is only read in untill the first \0 char? So that I need to append it, until it is really complete?</p>
]]></description><link>https://forum.qt.io/post/179002</link><guid isPermaLink="true">https://forum.qt.io/post/179002</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 13 May 2013 09:42:14 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 09:36:21 GMT]]></title><description><![CDATA[<p dir="auto">Hi SGaist,</p>
<p dir="auto">thank you, your answer was helpful!<br />
So now this works:<br />
@QByteArray ba;<br />
QBuffer buffer(&amp;ba);<br />
image.save(&amp;buffer, "PNG");</p>
<p dir="auto">QByteArray ba2 = buffer.data();<br />
const char *imageData = ba2.data();<br />
int imageSize = ba2.size();</p>
<p dir="auto">QByteArray ba3 = QByteArray(imageData, imageSize);<br />
QImage test;<br />
test.loadFromData(ba3);@</p>
<p dir="auto">and I send it like this:<br />
@QByteArray block;<br />
QDataStream out(&amp;block, QIODevice::WriteOnly);<br />
out.setVersion(QDataStream::Qt_5_0);<br />
out &lt;&lt; (quint16)0;<br />
out &lt;&lt; scaleFactor;<br />
out &lt;&lt; imageData;<br />
out &lt;&lt; imageSize;<br />
out.device()-&gt;seek(0);<br />
out &lt;&lt; (quint16)(block.size() - sizeof(quint16));<br />
tcpSocket-&gt;write(block);@</p>
<p dir="auto">and receive it so:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/qimage">@<bdi>QImage</bdi></a> img;<br />
double scaleFactor;<br />
char *imageData;<br />
const char *cImageData;<br />
int imageSize;</p>
<p dir="auto">in &gt;&gt; scaleFactor;<br />
in &gt;&gt; imageData;<br />
in &gt;&gt; imageSize;<br />
cImageData = imageData;<br />
qDebug() &lt;&lt; "scaleFactor:" &lt;&lt; scaleFactor &lt;&lt; "imageSize:" &lt;&lt; imageSize; //right numbers<br />
//this still doesn't work:<br />
QByteArray ba = QByteArray(cImageData, imageSize); //doesn't work with imageData, too<br />
img.loadFromData(ba);@</p>
<p dir="auto">Do you have any idea why the last two lines still don't work? "scaleFactor" and "imageSize" are transmitted correctly.</p>
]]></description><link>https://forum.qt.io/post/179000</link><guid isPermaLink="true">https://forum.qt.io/post/179000</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 13 May 2013 09:36:21 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Mon, 13 May 2013 09:23:38 GMT]]></title><description><![CDATA[<p dir="auto">Hi mcosta,</p>
<p dir="auto">when I write</p>
<p dir="auto">@in &gt;&gt; img;  //my QImage@</p>
<p dir="auto">I get this in the Application Output: "QIODevice::seek: Cannot call seek on a sequential device"</p>
]]></description><link>https://forum.qt.io/post/178991</link><guid isPermaLink="true">https://forum.qt.io/post/178991</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 13 May 2013 09:23:38 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Sat, 11 May 2013 20:23:46 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You need to send the data size of the image you are sending before the data so you can rebuild it on the other side.</p>
<p dir="auto">Using the QByteArray(const char *) constructor, your are telling it it's a string so it will stop at the first \0 char. You need to use the other constructor giving the size of the char array.</p>
<p dir="auto">Hope it helps</p>
]]></description><link>https://forum.qt.io/post/178875</link><guid isPermaLink="true">https://forum.qt.io/post/178875</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sat, 11 May 2013 20:23:46 GMT</pubDate></item><item><title><![CDATA[Reply to Sending infomartion like double and image with TCP on Sat, 11 May 2013 19:47:02 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">why you don't try with</p>
<p dir="auto">@<br />
out &lt;&lt; image; // your QImage object</p>
<p dir="auto">in &gt;&gt; image;<br />
@</p>
<p dir="auto">QImage implements<br />
@<br />
QDataStream&amp; operator &lt;&lt; (QDataStream &amp; str, const QImage&amp; img);<br />
QDataStream&amp; operator &gt;&gt; (QDataStream &amp; str, QImage&amp; img);<br />
@</p>
<p dir="auto">so you can write directly code like mine</p>
]]></description><link>https://forum.qt.io/post/178867</link><guid isPermaLink="true">https://forum.qt.io/post/178867</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Sat, 11 May 2013 19:47:02 GMT</pubDate></item></channel></rss>