<?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[Qthread and workers classes arguments]]></title><description><![CDATA[<p dir="auto">hi, I'm having some bad time trying understand how to use qthreads,<br />
I have some code to be runned   in a parallel thread, for that I used Qthread with classes, for that I used this page as sample <a href="https://wiki.qt.io/QThreads_general_usage" target="_blank" rel="noopener noreferrer nofollow ugc">QThreads general usage</a> .<br />
I took a wile to understand how implement this code is my first time implementing threads in codding.<br />
I'm managed to put the code running in the thread and worker but I need to pass some data from variables to the worker. I can't find any complete sample of "how to do it" I only find some brief explanations with code fragments and I'm not able to understand.</p>
<p dir="auto">how can I do that?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/topic/113469/qthread-and-workers-classes-arguments</link><generator>RSS for Node</generator><lastBuildDate>Wed, 20 May 2026 22:18:36 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/113469.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Apr 2020 02:47:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:09:04 GMT]]></title><description><![CDATA[<p dir="auto">Solved!!!!!!!!!!!!!!!!!!!!!</p>
<p dir="auto">I miss a default value...I replace:</p>
<pre><code>explicit Worker(QObject *parent = nullptr, int variable);
</code></pre>
<p dir="auto">by:</p>
<pre><code> explicit Worker(QObject *parent = nullptr, int variable = 0);
</code></pre>
<p dir="auto">And everything start working, there is my sample code for future references to noobs like me.</p>
<p dir="auto">mainwindow.cpp</p>
<pre><code>        QThread* threadtest = new QThread;
        Worker* worker = new Worker(nullptr, 100);

        worker-&gt;moveToThread(threadtest);
        connect(worker, SIGNAL (finished()), threadtest, SLOT (quit()));
        connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
        connect(threadtest, SIGNAL (finished()), threadtest, SLOT (deleteLater()));

        threadtest-&gt;start();
</code></pre>
<p dir="auto">worker.cpp</p>
<pre><code>Worker::Worker(QObject *parent, int variable) : QObject(parent)
{
    int intvar = variable;
}
</code></pre>
<p dir="auto">worker.h</p>
<pre><code>class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr, int variable = 0);
...
...
...
</code></pre>
]]></description><link>https://forum.qt.io/post/587670</link><guid isPermaLink="true">https://forum.qt.io/post/587670</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:09:04 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:14:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> thanks, that worked and simplify the code. thanks for help <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> and <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a></p>
]]></description><link>https://forum.qt.io/post/587674</link><guid isPermaLink="true">https://forum.qt.io/post/587674</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:14:21 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:10:11 GMT]]></title><description><![CDATA[<p dir="auto">As I already suggested, put parent last. You can have all the parameters without default values before the first that has one.</p>
]]></description><link>https://forum.qt.io/post/587672</link><guid isPermaLink="true">https://forum.qt.io/post/587672</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:10:11 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:09:04 GMT]]></title><description><![CDATA[<p dir="auto">Solved!!!!!!!!!!!!!!!!!!!!!</p>
<p dir="auto">I miss a default value...I replace:</p>
<pre><code>explicit Worker(QObject *parent = nullptr, int variable);
</code></pre>
<p dir="auto">by:</p>
<pre><code> explicit Worker(QObject *parent = nullptr, int variable = 0);
</code></pre>
<p dir="auto">And everything start working, there is my sample code for future references to noobs like me.</p>
<p dir="auto">mainwindow.cpp</p>
<pre><code>        QThread* threadtest = new QThread;
        Worker* worker = new Worker(nullptr, 100);

        worker-&gt;moveToThread(threadtest);
        connect(worker, SIGNAL (finished()), threadtest, SLOT (quit()));
        connect(worker, SIGNAL (finished()), worker, SLOT (deleteLater()));
        connect(threadtest, SIGNAL (finished()), threadtest, SLOT (deleteLater()));

        threadtest-&gt;start();
</code></pre>
<p dir="auto">worker.cpp</p>
<pre><code>Worker::Worker(QObject *parent, int variable) : QObject(parent)
{
    int intvar = variable;
}
</code></pre>
<p dir="auto">worker.h</p>
<pre><code>class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QObject *parent = nullptr, int variable = 0);
...
...
...
</code></pre>
]]></description><link>https://forum.qt.io/post/587670</link><guid isPermaLink="true">https://forum.qt.io/post/587670</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:09:04 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:03:01 GMT]]></title><description><![CDATA[<p dir="auto">You should put the parent parameter as last with the usual default value.</p>
<p dir="auto">Otherwise, that's correct.</p>
<p dir="auto">What error do you get ?</p>
]]></description><link>https://forum.qt.io/post/587667</link><guid isPermaLink="true">https://forum.qt.io/post/587667</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:03:01 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 19:01:21 GMT]]></title><description><![CDATA[<p dir="auto">main:</p>
<pre><code>Worker* worker = new Worker(nullptr, 10);
</code></pre>
<p dir="auto">w.cpp</p>
<pre><code>Worker::Worker(QObject *parent, int variable) : QObject(parent)
</code></pre>
<p dir="auto">w.h</p>
<pre><code>    explicit Worker(QObject *parent = nullptr, int variable);
</code></pre>
]]></description><link>https://forum.qt.io/post/587666</link><guid isPermaLink="true">https://forum.qt.io/post/587666</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 19:01:21 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 18:30:32 GMT]]></title><description><![CDATA[<p dir="auto">What did you try ?</p>
]]></description><link>https://forum.qt.io/post/587660</link><guid isPermaLink="true">https://forum.qt.io/post/587660</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 09 Apr 2020 18:30:32 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 18:27:19 GMT]]></title><description><![CDATA[<p dir="auto">hi, <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>, thanks for reply!</p>
<p dir="auto">Can you please sample some code? I already try but I don't know how to do it and i end every time with errors.</p>
<p dir="auto">thanks</p>
]]></description><link>https://forum.qt.io/post/587657</link><guid isPermaLink="true">https://forum.qt.io/post/587657</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 18:27:19 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 18:01:54 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">You have to declare a constructor that takes these parameters and not only the "QObject *parent".</p>
]]></description><link>https://forum.qt.io/post/587647</link><guid isPermaLink="true">https://forum.qt.io/post/587647</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 09 Apr 2020 18:01:54 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 17:58:04 GMT]]></title><description><![CDATA[<p dir="auto">Hi  <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>, thanks for reply!<br />
I can pass the parameters any time, but may for now its better pass it at the start, following your sample...if i pass the parameter when creating my worker how do i receive them?</p>
<p dir="auto">in mainwindow.cpp</p>
<pre><code>...
QThread* threadtest = new QThread;
MyWorker *worker = new MyWorker(param1, param2, ...);
...
</code></pre>
<p dir="auto">in worker.cpp</p>
<pre><code>...
Worker::Worker(QObject *parent) : QObject(parent)
{ 
   
}
...
</code></pre>
<p dir="auto">in worker.h</p>
<pre><code>...
public:
    explicit Worker(QObject *parent = nullptr);
...
</code></pre>
<p dir="auto">I Have no idea how to handle the parameters in worker.cpp and worker.h files to be able to receive them.<br />
How can I do it?</p>
]]></description><link>https://forum.qt.io/post/587646</link><guid isPermaLink="true">https://forum.qt.io/post/587646</guid><dc:creator><![CDATA[dedetuga]]></dc:creator><pubDate>Thu, 09 Apr 2020 17:58:04 GMT</pubDate></item><item><title><![CDATA[Reply to Qthread and workers classes arguments on Thu, 09 Apr 2020 06:33:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dedetuga">@<bdi>dedetuga</bdi></a> When exactly do you need to pass parameters?<br />
You can simply pass the parameters when you create your worker:</p>
<pre><code>MyWorker *worker = new MyWorker(param1, param2, ...);
</code></pre>
<p dir="auto">Or you can pass them later using signals/slots.</p>
]]></description><link>https://forum.qt.io/post/587535</link><guid isPermaLink="true">https://forum.qt.io/post/587535</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 09 Apr 2020 06:33:03 GMT</pubDate></item></channel></rss>