<?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[QSerialPort and QThread]]></title><description><![CDATA[<p dir="auto">I have the main MainWindow class that has instances of the class QThread and another is in charge of communications through the serial port. In the main class I initialize instances in .h as :</p>
<p dir="auto">Communications * com ;<br />
QThread * thread ;</p>
<p dir="auto">And in the constructor of the main class .cpp the initialize :</p>
<p dir="auto">com = new Comunications();<br />
thread = new QThread(this);</p>
<p dir="auto">I shake my communications class I created the thread above with<br />
com- &gt; moveToThread (thread) ;</p>
<p dir="auto">Also in this class i have the connections of signals and slots.<br />
When I go to write to the serial port jumps me an error:</p>
<p dir="auto">" QObject : Can not create children for a parent That is in a different thread. "</p>
<p dir="auto">Can someone help me??</p>
]]></description><link>https://forum.qt.io/topic/60379/qserialport-and-qthread</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 02:35:12 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/60379.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Nov 2015 17:09:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSerialPort and QThread on Fri, 06 Nov 2015 09:07:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<blockquote>
<p dir="auto">you also delete...</p>
</blockquote>
<p dir="auto">Yes definitely, I did not add that part but it should be deleted, will add a possible snippet to the code above, thanks</p>
]]></description><link>https://forum.qt.io/post/296907</link><guid isPermaLink="true">https://forum.qt.io/post/296907</guid><dc:creator><![CDATA[TheBadger]]></dc:creator><pubDate>Fri, 06 Nov 2015 09:07:59 GMT</pubDate></item><item><title><![CDATA[Reply to QSerialPort and QThread on Fri, 06 Nov 2015 08:52:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/thebadger">@<bdi>TheBadger</bdi></a> Do you also delete it when your thread stops ? Otherwise you have a memory leak here</p>
]]></description><link>https://forum.qt.io/post/296902</link><guid isPermaLink="true">https://forum.qt.io/post/296902</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 06 Nov 2015 08:52:51 GMT</pubDate></item><item><title><![CDATA[Reply to QSerialPort and QThread on Fri, 06 Nov 2015 09:13:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sergio">@<bdi>Sergio</bdi></a></p>
<p dir="auto">To follow up on what <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> said, what I normally do is not to create the serial port in the constructor of the <code>Comunications</code> class (assuming you do), but connect a slot in Communications to the <code>QThread::started()</code> signal, and in there create the serial port. That would ensure that when the serial port class is created that it will be created in the thread that needs to use it.</p>
<pre><code>class Comunications : public QObject {
public slots:
    void threadStarted() { 
        d_serialPort = new QSerialPort();
        // ... &lt;- set up
        d_serialPort.open();
    }
    void threadStopped() { 
        d_serialPort.close(); // Might not work/be needed
        delete d_serialPort;
        d_serialPort = nullptr;
    }
private:
    QSerialPort* d_serialPort;
}; 
</code></pre>
<p dir="auto">and then in your constructor</p>
<pre><code>com = new Comunications();
thread = new QThread(this);
connect(thread, &amp;QThread::started, com, &amp;Comunications::threadStarted);
thread-&gt;start();
</code></pre>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> pointed out, make sure to clean up objects created:</p>
<pre><code>connect(thread, &amp;QThread::finished, com, &amp;Comunications::threadStopped);
</code></pre>
<p dir="auto">PS: Please put your code into code blocks when creating posts<br />
[edit: added example code for cleanup]</p>
]]></description><link>https://forum.qt.io/post/296874</link><guid isPermaLink="true">https://forum.qt.io/post/296874</guid><dc:creator><![CDATA[TheBadger]]></dc:creator><pubDate>Fri, 06 Nov 2015 09:13:57 GMT</pubDate></item><item><title><![CDATA[Reply to QSerialPort and QThread on Thu, 05 Nov 2015 21:16:20 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/yoavmil">@<bdi>yoavmil</bdi></a> Are you trying to suggest to call moveToThread on an unallocated object ?</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sergio">@<bdi>Sergio</bdi></a> I guess your Communications class contains a QSerialPort ? If so, did you give a parent to your QSerialPort instance (i.e. <code>_serialPort = new QSerialPort(this)</code>)? From the error message, probably not, hence it's not moved along with your Communications instance.</p>
]]></description><link>https://forum.qt.io/post/296827</link><guid isPermaLink="true">https://forum.qt.io/post/296827</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 05 Nov 2015 21:16:20 GMT</pubDate></item><item><title><![CDATA[Reply to QSerialPort and QThread on Thu, 05 Nov 2015 20:18:59 GMT]]></title><description><![CDATA[<p dir="auto">yeah, happend to me too.<br />
the thing is, the any QObject 'knows' who are his childred, so the QSerialPort is alreay listed at the thread that created it, and you are trying to change it.<br />
try reordering the calls. first <code>moveToThread()</code>, then <code>com = new Comunications();</code></p>
]]></description><link>https://forum.qt.io/post/296814</link><guid isPermaLink="true">https://forum.qt.io/post/296814</guid><dc:creator><![CDATA[yoavmil]]></dc:creator><pubDate>Thu, 05 Nov 2015 20:18:59 GMT</pubDate></item></channel></rss>