<?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[how to create a new qthread on every call of the function and delete the old one on its completion]]></title><description><![CDATA[<p dir="auto">Hi there is a function 1 on whose call a signal is emitted and a new thread is also created. The connect part in function 1 connects the emitted signal and public slot of another class.</p>
<p dir="auto">The thread created in that function 1 should be destroyed after the completion of another class slot.</p>
<p dir="auto">And on next call to the function 1 a new thread should be created and so on.</p>
<p dir="auto"><strong>Currently the thread is not being destroyed.  How to fix this i.e. destroy the thread after slot completion.</strong></p>
<p dir="auto">Thank You !!!</p>
<pre><code>void EventListModel::vExtractNextItems()
{
    emit(fetch());

    t = new testthetimer();

    workerThread = new QThread();


    t-&gt;moveToThread(workerThread);

    connect(this, &amp;EventListModel::fetch, t, &amp;testthetimer::timeout);

    qInfo() &lt;&lt; "Function 1 call "&lt;&lt; QThread::currentThread();
    workerThread-&gt;start();

}
</code></pre>
<pre><code>void testthetimer::timeout()
{

    qInfo() &lt;&lt; QDateTime::currentDateTime().toString() &lt;&lt; " on " &lt;&lt; QThread::currentThread();



        qInfo() &lt;&lt; "total" &lt;&lt; QThread::idealThreadCount();
        this-&gt;thread()-&gt;quit();
        this-&gt;thread()-&gt;destroyed();

        qInfo() &lt;&lt; "running " &lt;&lt; this-&gt;thread()-&gt;isRunning();
        qInfo() &lt;&lt; QThread::currentThread();
}
</code></pre>
<p dir="auto">and the output is</p>
<pre><code>"Wed Oct 23 17:51:22 2019"  on  QThread(0x7ffe9c803970)
total 8
running  true
current thread is   QThread(0x7ffe9c803970)
Function 1 call  QThread(0x7ffe9b40efe0)
qml: DownScale
"Wed Oct 23 17:51:24 2019"  on  QThread(0x7ffe9b5de260)
total 8
running  true
current thread is   QThread(0x7ffe9b5de260)
Function 1 call  QThread(0x7ffe9b40efe0)
</code></pre>
]]></description><link>https://forum.qt.io/topic/108052/how-to-create-a-new-qthread-on-every-call-of-the-function-and-delete-the-old-one-on-its-completion</link><generator>RSS for Node</generator><lastBuildDate>Fri, 16 Oct 2026 05:01:27 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/108052.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 23 Oct 2019 16:31:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to how to create a new qthread on every call of the function and delete the old one on its completion on Wed, 23 Oct 2019 18:03:11 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Call deleteLater in your thread.<br />
Note that the cleaner way to do that is to emit a signal from your class and connect that to the QThread deleteLater method. This avoids that class to do any thread management that it should not really care about.</p>
<p dir="auto">Depending on your use case, you might want to consider the use of <a href="https://doc.qt.io/qt-5/qtconcurrent-index.html" target="_blank" rel="noopener noreferrer nofollow ugc">QtConcurrent</a>.</p>
]]></description><link>https://forum.qt.io/post/557620</link><guid isPermaLink="true">https://forum.qt.io/post/557620</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 23 Oct 2019 18:03:11 GMT</pubDate></item><item><title><![CDATA[Reply to how to create a new qthread on every call of the function and delete the old one on its completion on Thu, 24 Oct 2019 13:51:07 GMT]]></title><description><![CDATA[<p dir="auto">yes but with QtConcurrent. i am facing other problems please refer to <a href="https://forum.qt.io/topic/108088/how-to-replace-qthread-with-qtconcureent">https://forum.qt.io/topic/108088/how-to-replace-qthread-with-qtconcureent</a></p>
]]></description><link>https://forum.qt.io/post/557865</link><guid isPermaLink="true">https://forum.qt.io/post/557865</guid><dc:creator><![CDATA[Mandeep.Chaudhary]]></dc:creator><pubDate>Thu, 24 Oct 2019 13:51:07 GMT</pubDate></item><item><title><![CDATA[Reply to how to create a new qthread on every call of the function and delete the old one on its completion on Wed, 23 Oct 2019 18:03:11 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Call deleteLater in your thread.<br />
Note that the cleaner way to do that is to emit a signal from your class and connect that to the QThread deleteLater method. This avoids that class to do any thread management that it should not really care about.</p>
<p dir="auto">Depending on your use case, you might want to consider the use of <a href="https://doc.qt.io/qt-5/qtconcurrent-index.html" target="_blank" rel="noopener noreferrer nofollow ugc">QtConcurrent</a>.</p>
]]></description><link>https://forum.qt.io/post/557620</link><guid isPermaLink="true">https://forum.qt.io/post/557620</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Wed, 23 Oct 2019 18:03:11 GMT</pubDate></item></channel></rss>